tfont tfont - 1 month ago 8
MySQL Question

MySQL: comma separated string within IN()

I've seen a lot of questions with a similar question. However, all the answers are misleading. Most answers say to use FIND_IN_SET and this seems incorrect to my question.

Below query:

SELECT
*
FROM
data_table
WHERE field_id IN ('618,622,626,773,776');


Would normally just show the first ID row. In this example being '618'

id data
--- ------
618 ....


How would one go about this without using any stored procedures or user defined functions? Displaying the following results:

id data
--- ------
618 ....
622 ....
626 ....
773 ....
776 ....


Note; '618,622,626,773,776' will always remain as a string. This is the problem here.

Answer

Most answers say to use FIND_IN_SET and this is completely incorrect to my question.

It is completely correct. If it didn’t work for you – then you just used it wrong.

SELECT * FROM data_table WHERE FIND_IN_SET(field_id, '618,622,626,773,776')