开发者

regular expression search a number from string

开发者 https://www.devze.com 2023-03-30 19:39 出处:网络
this question maybe very simple for you. i\'m using mysql regexp statment开发者_运维技巧. myQuery is

this question maybe very simple for you. i'm using mysql regexp statment开发者_运维技巧.

myQuery is

select * from contents where categories regexp '{myPattern}';

categories field area 54,25,99,4,2... etc string.

my question how can i find only number of '4' from categories field.

sorry for my english. help me please.


… WHERE FIND_IN_SET('4', categories) > 0

better yet to normalize your db scheme with categories in their own table and then join these tables together m:n


The way to match "any cvs string containing '4' as a value in the string" is:

mycolumn regexp '[[:<:]]4[[:>:]]' 

In mysql regex, [[:<:]] means "start of word" and [[:>:]] means "end of word".


You can use this:

where categories RLIKE '(^|,)4($|,)';

This will match when categories contains 4, followed by a , or nothing, and precedeed by , or nothing.

0

精彩评论

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