python mysql select条件查询来自列表
2021-04-28 本文已影响0人
负负则正
试了N中方法,最终发现大神解决了。
https://blog.csdn.net/u011085172/article/details/79044490
list = [‘a’, ‘b’, ‘c’]
方法1,可行:cursor.execute("select *from students where pNo in (‘a’, ‘b’, ‘c’)”)
方法2,可行:sql= 'select * from students where pNo in (%s)' %','.join(['%s']* len(list))
cursor.execute(sql,list)
print(sql)# select* from students where pNo in (%s,%s,%s)
方法3,不可行,原因我也没明白:
b= "','"
list2= b.join(list)
list3= "'"+list2+"'"
sql2="select * from students where pNo in (%s) "
cursor.execute(sql2,list3)