Logan
Logan

Reputation: 1694

regular expression in mysql query

I have mysql table values like

1-2

1-2-3

1-4-5

1-4-5-12-15

15-45-75

1-5-15-25-35-55

55-65-75

I want to select which rows have number 5(not 15 or 25 or 35).

I have tried with LIKE query but it gives all 5 value contains rows(including 15, 35, 45,55).

SELECT ... WHERE linkage LIKE '%5%'

I think we can do it through REGEXP. Can you help me for it?

Upvotes: 4

Views: 150

Answers (2)

Joe G Joseph
Joe G Joseph

Reputation: 24106

try

SELECT ... WHERE concat('-',linkage,'-') LIKE '%-5-%'


SQL Fiddle Demo

Upvotes: 2

Omesh
Omesh

Reputation: 29121

You can do this using FIND_IN_SET:

SELECT ... 
WHERE FIND_IN_SET(5, REPLACE(linkage,'-',','));

Example @ sqlfiddle

Upvotes: 7

Related Questions