search

Home  >  Q&A  >  body text

MySQL: Find columns matching integers in a text (CSV) column

This table has a text column that stores references to other records in CSV format, for example:

+----+----------------+
| id | linked_folders |
+----+----------------+
| 90 |    NULL        |
| 91 |    NULL        |
| 92 |    123,1,4,40  |
| 93 |    123,1       |
| 94 |    NULL        |
| 95 |    235,8       |
| 96 |    90          |
| 97 |    NULL        |
| 98 |    NULL        |
| 99 |    NULL        |
+----+----------------+

$id = 90;
SELECT * FROM my_table WHERE id = $id OR $id is in linked_folders

The above pseudo query should return rows 90 and 96.

I want to match if a certain exact value I have is on this field.

I'm thinking using LIKE might not work because I don't know if the comma exists before or after it.

Do I have any alternatives?

P粉336536706P粉336536706228 days ago400

reply all(1)I'll reply

  • P粉885035114

    P粉8850351142024-04-05 14:04:01

    Can be achieved using FIND_IN_SET:

    select * 
    from my_table 
    where id = 90 or FIND_IN_SET(90, linked_folders) > 0

    Demo here

    reply
    0
  • Cancelreply