我正在使用PyPika构建SQL查询。我想根据来自外部方(account_list)的输入动态添加'OR‘子句。我没有看到任何关于如何做到这一点或者这是否可能的文档。
示例:
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_list填充,而不管列表中有多少?
非常提前感谢您。
发布于 2020-10-15 00:56:12
在将其传递给Criterion.any之前,您可以简单地在列表中预先构建条件。
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))
)https://stackoverflow.com/questions/62524188
复制相似问题