📜  SQLAlchemy核心-使用连接词

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


联结是SQLAlchemy模块中的函数,这些函数实现SQL表达式的WHERE子句中使用的关系运算符。运算符AND,OR,NOT等用于形成组合两个单独的逻辑表达式的复合表达式。在SELECT语句中使用AND的简单示例如下-

SELECT * from EMPLOYEE WHERE salary>10000 AND age>30

SQLAlchemy函数and _(),or_()和not_()分别实现AND,OR和NOT运算符。

and_()函数

它产生由AND连接的表达式的合取。为了更好的理解下面给出一个例子-

from sqlalchemy import and_

print(
   and_(
      students.c.name == 'Ravi',
      students.c.id <3
   )
)

这转化为-

students.name = :name_1 AND students.id < :id_1

要在学生表的select()构造中使用and_(),请使用以下代码行-

stmt = select([students]).where(and_(students.c.name == 'Ravi', students.c.id <3))

具有以下性质的SELECT语句将被构造-

SELECT students.id, 
   students.name, 
   students.lastname
FROM students
WHERE students.name = :name_1 AND students.id < :id_1

显示上述SELECT查询输出的完整代码如下-

from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, ForeignKey, select
engine = create_engine('sqlite:///college.db', echo = True)
meta = MetaData()
conn = engine.connect()

students = Table(
   'students', meta, 
   Column('id', Integer, primary_key = True), 
   Column('name', String), 
   Column('lastname', String), 
)

from sqlalchemy import and_, or_
stmt = select([students]).where(and_(students.c.name == 'Ravi', students.c.id <3))
result = conn.execute(stmt)
print (result.fetchall())

假设学生表中填充了前面示例中使用的数据,则将选择下一行-

[(1, 'Ravi', 'Kapoor')]

or_()函数

它产生由OR连接的表达式的合取。我们将使用or_()将下面示例中的stmt对象替换为以下示例

stmt = select([students]).where(or_(students.c.name == 'Ravi', students.c.id <3))

这实际上等效于以下SELECT查询-

SELECT students.id, 
   students.name, 
   students.lastname
FROM students
WHERE students.name = :name_1 
OR students.id < :id_1

一旦进行替换并运行上面的代码,结果将是两行属于OR条件-

[(1, 'Ravi', 'Kapoor'),
(2, 'Rajiv', 'Khanna')]

asc()函数

它产生一个升序的ORDER BY子句。函数采用该列将函数用作参数。

from sqlalchemy import asc
stmt = select([students]).order_by(asc(students.c.name))

该语句实现以下SQL表达式-

SELECT students.id, 
   students.name, 
   students.lastname
FROM students 
ORDER BY students.name ASC

以下代码按名称列的升序列出了学生表中的所有记录-

from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String, ForeignKey, select
engine = create_engine('sqlite:///college.db', echo = True)
meta = MetaData()
conn = engine.connect()

students = Table(
   'students', meta, 
   Column('id', Integer, primary_key = True), 
   Column('name', String), 
   Column('lastname', String), 
)

from sqlalchemy import asc
stmt = select([students]).order_by(asc(students.c.name))
result = conn.execute(stmt)

for row in result:
   print (row)

上面的代码产生以下输出-

(4, 'Abdul', 'Sattar')
(3, 'Komal', 'Bhandari')
(5, 'Priya', 'Rajhans')
(2, 'Rajiv', 'Khanna')
(1, 'Ravi', 'Kapoor')

desc()函数

类似的desc()函数产生降序的ORDER BY子句,如下所示:

from sqlalchemy import desc
stmt = select([students]).order_by(desc(students.c.lastname))

等效的SQL表达式是-

SELECT students.id, 
   students.name, 
   students.lastname
FROM students 
ORDER BY students.lastname DESC

以上代码行的输出是-

(4, 'Abdul', 'Sattar')
(5, 'Priya', 'Rajhans')
(2, 'Rajiv', 'Khanna')
(1, 'Ravi', 'Kapoor')
(3, 'Komal', 'Bhandari')

between()函数

它产生一个BETWEEN谓词子句。通常用于验证某列的值是否在一个范围内。例如,以下代码选择id列在2到4之间的行-

from sqlalchemy import between
stmt = select([students]).where(between(students.c.id,2,4))
print (stmt)

产生的SQL表达式类似于-

SELECT students.id, 
   students.name, 
   students.lastname
FROM students
WHERE students.id 
BETWEEN :id_1 AND :id_2

结果如下-

(2, 'Rajiv', 'Khanna')
(3, 'Komal', 'Bhandari')
(4, 'Abdul', 'Sattar')