Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyPika创建动态Where语句_Python_Sql_Pypika - Fatal编程技术网

Python PyPika创建动态Where语句

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

我正在使用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, 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))
)