Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write Regular Expression in MySQL select queries?

Tags:

regex

php

mysql

I tried this expression /\b(word\w*)\b/i to compare a word against the list of other words to find the duplicates. I used preg_math_all() and it worked fine. I wanted to do the same thing but this time check against the words retrieved from mysql database. This is what i wrote

SELECT * FROM table WHERE word = /\b(word\w*)\b/i

this didnt worked.

like image 765
daron Avatar asked Dec 05 '10 04:12

daron


2 Answers

Your query should look like:

SELECT * FROM table WHERE column REGEXP '/\b(word\w*)\b/i'
like image 151
esqew Avatar answered Oct 24 '22 17:10

esqew


MySQL regex syntax is a different from PHP's, you don't need the delimiters at the start and end.

SELECT * FROM table WHERE word REGEXP '\b(word\w*)\b'

You can't use the i flag for case insensitivity the same way as in PHP, as in MySQL whether the regex is case sensitive or not depends on whether the column collation is a case sensitive one or not.

like image 37
Michael Low Avatar answered Oct 24 '22 19:10

Michael Low