问题
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