【问题标题】:Put multiple int in the where clause of sql在sql的where子句中放入多个int
【发布时间】:2022-06-16 22:15:59
【问题描述】:
import pandas as pd

sql_query = '''
Select *
from datamart_table
where quantity in (%s)
'''
x = [10, 20, 30]
placeholders = ', '.join(i for i in x)  

SQL = sql_query%(placeholders)
df = pd.read_sql(SQL, connection)

上面是我的sn-p。我希望在数量在 list_x 中时获取。下面是我得到的错误。我不希望将 x 中的值转换为字符串。任何出路。

TypeError:序列项 0:预期的 str 实例,找到 int

【问题讨论】:

    标签: python mysql


    【解决方案1】:

    每个错误都需要一个字符串:

    placeholders = ', '.join(str(i) for i in x)  
    

    【讨论】:

    • 根据问题:我不希望将 x 中的值转换为字符串......原因是该列在 int 中......所以对查询性能不利
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 2019-02-09
    • 2013-05-21
    • 2018-09-11
    • 1970-01-01
    相关资源
    最近更新 更多