prashant
prashant

Reputation: 966

MySql REGEXP to match two comma separated strings

I have a table containing following values :

id    |    value      |
-----------------------
1     | 1,2,5,8,12,20 |    
2     | 11,25,26,28   |    
-----------------------

now I want to search some comma separated IDs e.g. '1,3,6,7,11' from above value column e.g.

SELECT id FROM tbl_name  
WHERE value REGEXP '*some reg exp goes here containing 1,3,6,7,11*'
LIMIT 1,0;

SELECT id FROM tbl_name  
WHERE value REGEXP '*some reg exp goes here containing 3,6,27,15*'
LIMIT 1,0;

above 1st query should return 1 while the 2nd should return NULL

I am new with regular expressions can anyone help. Thanks

Upvotes: 6

Views: 7247

Answers (1)

Michel Feldheim
Michel Feldheim

Reputation: 18250

REGEXP '(^|,)(1|3|6|7|11)(,|$)'

Will match all values containing one number of the sequence 1,3,6,7,11.

You should not use one column to save several values. Normalize data!

Edited answer

Upvotes: 18

Related Questions