使用变量进行IN操作

时间:2016-06-30 13:04:42

标签: mysql

如何在IN查询中使用以下变量内容...

SET @var = '1,2,3';
SELECT * FROM Table WHERE ID IN (@var);

2 个答案:

答案 0 :(得分:1)

使用JOIN

SELECT t1.* 
FROM Table t1
JOIN Table t2 on t1.OtherID = t2.OtherID
WHERE t2.X = 1

答案 1 :(得分:0)

通过查看this answer

找到解决方案
SET @var = '1,2,3';
SET @Expression=CONCAT('SELECT * FROM TableWHERE ID IN (',@var,')');
PREPARE myquery FROM @Expression;
EXECUTE myquery