If I understand you correctly (I'm still not sure I do), you could just use:
SELECT * FROM table WHERE column LIKE '%1_2_3%';
This would give you columns where the value is like:
1_2_3_4_5_5
1_4_5_, 1_2_3_4_5_, 6_7
and so on.
But you should really normalize your tables. This is important for good queries and performance wise also important.
According to @Xatenev suggestions, if you really like only the values and the row of each matching row, this won't work so well and will be a lot of overhead. This are the steps that I would perform:
- Split all CSV columns into multiple rows (this is a hack and a performance killer, I found some working solution but did not test it, see here): Pseudo Code:
SELECT ID, SPLIT(',', column) AS entries FROM table
(NOT WORKING)
- Filter the new virtual table to select only rows that match the prefix (
SELECT * FROM virtual_table WHERE find_in_set("1_2_3%, entries) ORDER BY ID
)
- Concatenate the matching
entries
back into a list for each ID
. e.g. SELECT ID, GROUP_CONCAT(entries SEPARATOR ', ') FROM filtered_table GROUP BY ID
- Do something
The unknown part is the beginning with the split in multiple rows. There are a lot of possible solutions all with their own drawbacks or advantages. Be aware that this will always (regardless of the selected method) will cost a lot of performance.
ADDITIONAL NODE:
It could be adventures in your situation, that you get each row matching your search string like in my first example and filter them in memory. This might be faster than doing this in MYSQL.