开发者

How to match 2 out of "1,2,7,9,13,3,10,4,21,6,12" in MySQL?

I'm n开发者_开发问答ot familiar with regex in MySQL.


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).


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'


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.)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜