Python PyPika创建动态Where语句
我正在使用PyPika构建SQL查询。我想根据外部方(帐户列表)的输入动态添加'OR'子句。我没有看到任何关于如何做到这一点或是否可行的文档 例如:Python PyPika创建动态Where语句,python,sql,pypika,Python,Sql,Pypika,我正在使用PyPika构建SQL查询。我想根据外部方(帐户列表)的输入动态添加'OR'子句。我没有看到任何关于如何做到这一点或是否可行的文档 例如: from pypika import Query, Table, Field, CustomFunction, Order, Criterion account_list = [ '23456', '49375', '03948' ] Query.from_(my_table).select(my_table.product_code, ac
from pypika import Query, Table, Field, CustomFunction, Order, Criterion
account_list = [ '23456', '49375', '03948' ]
Query.from_(my_table).select(my_table.product_code, account, ) \
.where( ( my_table.product_code.like('product_1%') | \
my_table.product_code.like('product_2%') ) ) \
.where( Criterion.any([my_table.account == '23456', \
my_table.account == '49375', my_table.account == '03948']) )
无论列表中有多少,是否可以从account_列表填充标准值
非常感谢您。您只需事先在列表中建立标准,然后再将其传递给
标准。任何
account_list = [ '23456', '49375', '03948' ]
account_criterions = [my_table.account == account for account in account_list]
query = (
Query.from_(my_table)
.select(my_table.product_code, account)
.where(my_table.product_code.like('product_1%') | my_table.product_code.like('product_2%'))
.where(Criterion.any(account_criterions))
)