phpmysql

MySQL - How to select rows where value is in array?


Ok, normally I know you would do something like this if you knew the array values (1,2,3 in this case):

SELECT * WHERE id IN (1,2,3)

But I don't know the array value, I just know the value I want to find is 'stored' in the array:

SELECT * WHERE 3 IN (ids) // Where 'ids' is an array of values 1,2,3

Which doesn't work. Is there another way to do this?


Solution

  • Use the FIND_IN_SET function:

    SELECT t.*
      FROM YOUR_TABLE t
     WHERE FIND_IN_SET(3, t.ids) > 0