Don't do that. Dynamic SQL is bad news.
Every RDBMS has this issue, because stored procedures are not able to use a dynamic list. The way to work this out changes by RDBMS, but all of them are ways it split the values in SQL itself.
Anyway, a simple answer would be to use a recursive CTE to split the values into separate records. Once that is done, IN() or EXISTS can SELECT from the entire CTE.
Here's the basic idea. I quickly looked at mysql functions to write this, but have not tested it at all. Hopefully it shows the basic idea:
WITH RECURSIVE split_csv(x, rest)
AS ( SELECT SUBSTRING_INDEX(?, ',', 1), SUBSTRING(?, FROM INST
+R(?, ',') + 1)
UNION ALL SELECT SUBSTRING_INDEX(rest, ',', 1), SUBSTRING(rest, FROM I
+NSTR(?, ',') + 1)
FROM split_csv)
SELECT something FROM atable WHERE data in (SELECT x FROM split_csv);
|