Code
'''
System version : Windows 10 64-bit
Python version : Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
'''
id_list = ['a','b']
sql_id_list = str(tuple([key for key in id_list])).replace(',)', ')')
query ="""
SELECT id FROM table WHERE article_id IN {sql_id_list}
""".format(sql_id_list=sql_id_list)
print("sql_id_list:",sql_id_list)
print("query:",query)
id_list = ['a']
sql_id_list = str(tuple([key for key in id_list])).replace(',)', ')')
query ="""
SELECT id FROM table WHERE article_id IN {sql_id_list}
""".format(sql_id_list=sql_id_list)
print("sql_id_list:",sql_id_list)
print("query:",query)
Result
sql_id_list: ('a', 'b')
query:
SELECT id FROM table WHERE article_id IN ('a', 'b')
sql_id_list: ('a')
query:
SELECT id FROM table WHERE article_id IN ('a')
沒有留言:
張貼留言