📜  公共关系运算符

📅  最后修改于: 2020-11-27 07:52:44             🧑  作者: Mango


在本章中,我们将讨论基于关系的运算符。

__eq __()

上面的运算符是多对一的“相等”比较。该运算符的代码行如下所示:

s = session.query(Customer).filter(Invoice.invno.__eq__(12))

以上代码行的等效SQL查询为-

SELECT customers.id 
AS customers_id, customers.name 
AS customers_name, customers.address 
AS customers_address, customers.email 
AS customers_email
FROM customers, invoices
WHERE invoices.invno = ?

__ne __()

此运算符是多对一的“不等于”比较。该运算符的代码行如下所示:

s = session.query(Customer).filter(Invoice.custid.__ne__(2))

上面的代码行的等效SQL查询如下-

SELECT customers.id 
AS customers_id, customers.name 
AS customers_name, customers.address 
AS customers_address, customers.email 
AS customers_email
FROM customers, invoices
WHERE invoices.custid != ?

contains()

该运算符用于一对多集合,下面给出了contains()的代码-

s = session.query(Invoice).filter(Invoice.invno.contains([3,4,5]))

以上代码行的等效SQL查询为-

SELECT invoices.id 
AS invoices_id, invoices.custid 
AS invoices_custid, invoices.invno 
AS invoices_invno, invoices.amount 
AS invoices_amount
FROM invoices
WHERE (invoices.invno LIKE '%' + ? || '%')

任何()

any()运算符用于集合,如下所示-

s = session.query(Customer).filter(Customer.invoices.any(Invoice.invno==11))

上面的代码行的等效SQL查询如下所示-

SELECT customers.id 
AS customers_id, customers.name 
AS customers_name, customers.address 
AS customers_address, customers.email 
AS customers_email
FROM customers
WHERE EXISTS (
   SELECT 1
   FROM invoices
   WHERE customers.id = invoices.custid 
   AND invoices.invno = ?)

具有()

此运算符用于标量引用,如下所示-

s = session.query(Invoice).filter(Invoice.customer.has(name = 'Arjun Pandit'))

以上代码行的等效SQL查询为-

SELECT invoices.id 
AS invoices_id, invoices.custid 
AS invoices_custid, invoices.invno 
AS invoices_invno, invoices.amount 
AS invoices_amount
FROM invoices
WHERE EXISTS (
   SELECT 1
   FROM customers
   WHERE customers.id = invoices.custid 
   AND customers.name = ?)