【发布时间】:2017-05-08 22:19:07
【问题描述】:
我目前正在构建一个 SQL 查询(在 Python 中):
query = "SELECT * FROM Table WHERE (" + \
" OR ".join([firstType+'=1' for firstType in firstTypes]) + \
") AND (" + \
" OR ".join([secondType+'=1' for secondType in secondTypes]) + \
")"
给定两个列表 firstType=['B','F'] 和 secondType=['a','d'] 生成查询
SELECT * FROM Table WHERE ( ('B'=1 OR 'F'=1) AND ('a'=1 OR 'd'=1) )
我使用Table.raw(query) 执行。
我知道如何在 Peewee 中生成特定查询,例如:
Table.select().where( (Table.B=1 | Table.F=1) & (Table.a=1 | Table.d=1) )
但问题是我事先不知道这两个列表(即列)的内容。
如何基于仅在运行时才知道的(可能有很多)列动态构建 Peewee 查询?
【问题讨论】: