【发布时间】:2021-03-04 15:35:32
【问题描述】:
我想运行一个查询,根据两个列表中的值过滤两列。
基本上,我想模拟两个这样的过滤器:
SELECT *
FROM my_table
WHERE customers in ("John","Peter") AND customers_numbers IN ('1','2')
但是来自customers 和customers_number 的值在两个列表中。为了尝试这个,我正在编写以下代码:
list1 = ["John","Peter"]
list2 = [1,2]
query_sql = "DELETE FROM vw_bill_details WHERE customers in (%s) and customers_numbers in (%s)" % ','.join(['?'] * len(list1)) % ','.join(['?'] * len(list2))
cursor.execute(query_sql, list1,list2)
但我收到以下错误:
query_sql = "DELETE FROM vw_bill_details WHERE customers in (%s) and customers_numbers in (%s)" % ','.join(['?'] * len(list1)) % ','.join(['?'] * len(list2))
TypeError: not enough arguments for format string
如何使用 python 进行上述查询?
谢谢!
【问题讨论】:
标签: python sql list filter cursor