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
2009-12-16 07:46:19
I need a solution that will work in all cases.
2009-12-16 07:49:17
Updated! you could use OR
S.Mark
2009-12-16 07:55:48
+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
2009-12-16 09:02:08
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
2009-12-16 09:09:48
+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
2009-12-16 09:15:02
+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
2009-12-16 09:18:25
agreed, the poster is probably using "list of ids" to represent 1:M relation
stereofrog
2009-12-16 09:27:06