In MySQL how do I use a user defined variable in a WHERE IN clause?

房东的猫 提交于 2019-12-13 08:53:17

问题


I don't understand why this does not work. Can someone explain what I need to do?

SET @my_list = '2,6,8,10,12,13,14,18,19,21';

DELETE FROM my_table WHERE my_table.table_id IN (@my_list);

回答1:


It's interpreting @my_list as a single id and so you're asking it to delete from my_table where your id is the string "2,6,8,10,12,13,14,18,19,21"

You could try

SET @my_list = '2,6,8,10,12,13,14,18,19,21';
SET @exec = CONCAT('DELETE FROM my_table WHERE my_table.table_id IN (', @my_list ,')');
EXECUTE (@exec);


来源:https://stackoverflow.com/questions/7815031/in-mysql-how-do-i-use-a-user-defined-variable-in-a-where-in-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!