我正在尝试优化这段代码以使用单个查询,而不是一遍又一遍地循环。
while not (dataX):
i += 1
this_id = '/'.join(this_id.split('/')[0:-i])
if not this_id:
break
else:
dataX = db.conn[db_read].query("SELECT x AS xX FROM link WHERE _deleted = 0 AND _ref = %s AND _ntype = 'code' LIMIT 1;", data = (this_id,))
我想将 IN 子句与包含所有可能的子字符串的变量一起使用,但我无法让它工作。
this_id_list = "'/a/b/c/d/e' , '/a/b/c/d', '/a/b/c', '/a/b', '/a'"
result = db.conn[db_read].query("SELECT x AS xX FROM link WHERE _deleted = 0 AND _ref IN($this_id_list)")
知道我做错了什么以及如何解决它吗?我真的很感激任何意见!顺便说一句,这是一个 Python 脚本。
最佳答案
this_id_list = "'/a/b/c/d/e' , '/a/b/c/d', '/a/b/c', '/a/b', '/a'"
这应该是一个字符串
关于python - 如何在 MySQL 的 IN 子句中使用变量。 Python脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46349247/