maverabil
maverabil

Reputation: 188

regular expression search a number from string

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.

Upvotes: 0

Views: 785

Answers (3)

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99929

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.

Upvotes: 0

Bohemian
Bohemian

Reputation: 425448

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

Upvotes: 1

knittl
knittl

Reputation: 265966

… 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

Upvotes: 6

Related Questions