M.S. M.S. - 6 months ago 41
MySQL Question

Filter records using a comma separated string

I've a string variable

and I'm trying to filter the records from table
but nothing helping me out since I'm new to MySql.

SET @IDS = '1,2,3';
select * from user where find_in_set(@IDS,ID);


You need to switch the order of the parameters:

FROM user

From the documentation:

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings

In other words, the first parameter is the string you are trying to find in the CSV list of strings which is the second parameter. If ID can be found in @IDS, then the index (beginning at one) which matches will be returned. If no match be found, then zero will be returned.