MySQL: Find columns matching integers in a text (CSV) column
P粉336536706
P粉336536706 2024-04-04 18:03:46
0
1
295

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粉336536706
P粉336536706

reply all(1)
P粉885035114

Can be achieved using FIND_IN_SET:

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

Demo here

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!