Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression in mysql query

Tags:

sql

mysql

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?

like image 392
Logan Avatar asked Jan 16 '23 16:01

Logan


2 Answers

You can do this using FIND_IN_SET:

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

Example @ sqlfiddle

like image 160
Omesh Avatar answered Jan 25 '23 16:01

Omesh


try

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


SQL Fiddle Demo

like image 20
Joe G Joseph Avatar answered Jan 25 '23 14:01

Joe G Joseph