Going crazy trying to set a variable in a query of type:
SET @idcamposexcluidos='817,803,495';
so i can then use it on a
WHERE id_campo not in (@idcamposexcluidos)
I’ve tried defining the variable in different formats with no luck and don’t seem to find an specific example for the above:
SET @idcamposexcluidos='(817,803,495)';
...
WHERE id_campo not in @idcamposexcluidos
SET @idcamposexcluidos=817,803,495;
with no success. It either returns an error or ignores the values.
You can’t use the
INclause like that. It compiles to a single string in yourINclause. But anINclause needs separate values.compiles to
but it should be
To overcome this either use dynamic SQL or in MySQL you could use FIND_IN_SET:
but using a function like
FIND_IN_SET()can not make use of indexes.