tags:

views:

138

answers:

4

I'm not familiar with regex in MySQL.

+3  A: 

LIKE '%,2,%' to match in the middle, LIKE '2,%' to match at start, LIKE '%,2' to match at end and to exact match, you can use = '2'

UPDATE: To work all cases, you could use OR, X LIKE '%,2,%' OR X LIKE '2,%' OR X LIKE '%,2' OR X='2'

S.Mark
I need a solution that will work in all cases.
Updated! you could use OR
S.Mark
+1  A: 
SELECT '1,2,7,9,13,3,10,4,21,6,12' REGEXP '(^2$)|(^2,)|(,2,)|(,2$)' AS matches

It can probably be fine tuned but it should work.

P.S. Please don't use the subject to write the whole question

Álvaro G. Vicario
not sure about MySQL, but `\b2\b` can work well here. Also, please don't use the **answer** to *comment* on the question, unless it is relevant.
Kobi
+8  A: 

For testing if the value exists in the string you can use

mysql> SELECT FIND_IN_SET(15, '1,2,15,4,5,6');
+---------------------------------+
| FIND_IN_SET(15, '1,2,15,4,5,6') |
+---------------------------------+
|                               3 |
+---------------------------------+
1 row in set (0.00 sec)

and test it for greater than 0 (0 is returned if no match is found).

Tor Valamo
this is the correct answer
stereofrog
+3  A: 

What problem are you really trying to solve, here, though? This smells like bad design.

(I don't see any comment box, perhaps due to lack of rep --- therefore posted as an answer.)

Alex Brasetvik
agreed, the poster is probably using "list of ids" to represent 1:M relation
stereofrog