I have a table in MySQL, like below:
------------------------
id | codes
------------------------
1 | 1,2,3,4
------------------------
2 | 1,4
------------------------
3 | 1,2,4
------------------------
4 | 1
------------------------
- Example-1: select all case that
codes
column contains 1 and 4, then the result is below (because the id=4 row thatcodes
column not contain 4):
------------------------
id | codes
------------------------
1 | 1,2,3,4
------------------------
2 | 1,4
------------------------
3 | 1,2,4
------------------------
- Example-2: select all case that
codes
column contains 2 or 3, then the result is below (because the id=4, id=2 rows thatcodes
column not contain any value in 2,3):
------------------------
id | codes
------------------------
1 | 1,2,3,4
------------------------
3 | 1,2,4
------------------------
Question: But I don't how to write sql to do that if codes stores lots of value such as 1,2,3,..20,21
.
I tried this:
SELECT SQL_NO_CACHE * FROM table WHERE
codes like '%1%4%'
and codes REGEXP'[2|3]';
Is there any better way to do that?