WHERE子句中的Python MySQL executemany [英] Python MySQL executemany in WHERE clause
本文介绍了WHERE子句中的Python MySQL executemany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要从MySQL中选择值,如下所示
I want to select values from MySQL as follows
do_not_select = [1,2,3]
cursor = database.cursor()
cursor.executemany("""SELECT * FROM table_a WHERE id != %s""",(do_not_select))
data = cursor.fetchall()
查询返回数据库中除第一个ID(1)外的所有值.我不希望它选择ID 1,2或3.
The query return all the values in the db apart form the first id (1). I don't want it to select id 1,2 or 3 however.
是否可以使用executemany命令?.
Is this possible using the executemany command..?
推荐答案
放弃NOT IN
:
do_not_select = [1, 2, 3]
cursor.execute("""SELECT * FROM table_a
WHERE id NOT IN ({}, {}, {})""".format(do_not_select[0],
do_not_select[1],
do_not_select[2]))
data.cursor.fetchall()
我怀疑(尽管我尚未测试过)ID_do_not_select是一个元组会更好,然后我认为,您可以直接将其触发到您的查询中:
I suspect (though I haven't tested this) that this would work better id do_not_select was a tuple, then I think you could just fire it straight into your query:
do_not_select = (1, 2, 3)
cursor.execute("""SELECT * FROM table_a
WHERE id NOT IN {}""".format(do_not_select))
data.cursor.fetchall()
我想知道这是否有效-如果您尝试尝试,请告诉我:)
I'd be interested to know if this works - if you try it please let me know :)
这篇关于WHERE子句中的Python MySQL executemany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文