【问题标题】:Pyathena SQL Query with Python condition带有 Python 条件的 Pyathena SQL 查询
【发布时间】:2021-10-23 14:43:45
【问题描述】:

给出以下python列表:

customer_list = [123,567,494]

现在我想运行一个使用上面列表的 SQL 查询。如何将条件 in (customer_list) 添加到我的查询中?

我试过了:

my_query = """
select * from my_table
where customer in ("""+customer_list")" 
"""
order by name
"""

这给了我错误: TypeError: 一元 + 的操作数类型错误:'str'

【问题讨论】:

    标签: python sql pyathena


    【解决方案1】:

    你可以试试这样的:

    customer_list = [123,567,494]
    
    my_query = """
    select * from my_table
    where customer in ({cust_list}) 
    order by name
    """.format(cust_list=",".join(str(x) for x in customer_list))
    

    输出:

    select * from my_table where customer in (123,567,494) order by name
    

    【讨论】:

      【解决方案2】:

      您可以在查询中将 customer_list 替换为:

      ', '.join(str(element) for element in customer_list)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        • 2022-07-07
        • 2021-03-21
        • 2016-07-26
        • 1970-01-01
        • 2023-03-22
        • 2011-09-10
        相关资源
        最近更新 更多