在where子句中使用list comprehension:
from sqlalchemy import and_,or_,select
stmt = select(
[example.c.id,example.c.data]
).where(or_(and_(example.c.id==data[0],example.c.date==data[1])
for data in interesting_data))
但是,我注意到的另一个问题是您将日期列与字符串数据类型进行比较. interesting_data列表应该是
import datetime as dt
interesting_data = (
(1,dt.date(2016,5,1)),6,)
另请注意,可以创建基本语句,然后逐步向其添加子句,从而导致(希望)更好的易读性和代码重用.
因此,可以将上面的内容写成
base_stmt = select([example.c.id,example.c.data])
wheres = or_(and_(example.c.id==data[0],example.c.date==data[1])
for data in interesting_data))
stmt = base_stmt.where(wheres)
这会生成以下sql(由我添加的新行和空格):
SELECT example.id,example.date,example.data
FROM example
WHERE
example.id = :id_1 AND example.date = :date_1
OR example.id = :id_2 AND example.date = :date_2
OR example.id = :id_3 AND example.date = :date_3
OR example.id = :id_4 AND example.date = :date_4
OR example.id = :id_5 AND example.date = :date_5
注意:如果要像这样过滤很多行,那么创建临时表,从interesting_data向此临时表插入行,然后将内部连接插入此表可能更有效,而不是如图所示添加where子句以上.