[英]Use of IN clause in SQL Server
我需要通過執行以下內容從數據庫中獲取數據:
query2 = """ select Comment, CONVERT(RUNID, CHAR(20)) AS RUNID from abc where Comment like '{}' and RUNID IN %s """.format(form['error_message'], id_list)
cursor = connections['xyz'].cursor()
cursor.execute(query2)
這里 form['error_message'] 是一個字符串,而 id_list 是一個字符串列表。 我構建的查詢導致錯誤。 我哪里錯了? 任何幫助表示贊賞!
請嘗試以下操作:
quoted_ids = list(map(lambda x:"'{}'".format(x),id_list))
query2 = """select Comment, CONVERT(RUNID, CHAR(20)) AS RUNID from abc where Comment like '{0}' and RUNID IN ({1}) """.format(form["error_message"],",".join(quoted_ids))
cursor = connections['xyz'].cursor()
cursor.execute(query2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.