Read the Web Design Blog

Regexp in MySQL

Sometimes I have to make mini search engines. Several engines later, I still never realized I could use regex in MySQL. Ok, this is more for me than you I suppose, but I found the information so interesting I had to save it here: The basic syntax to use regular expressions in a MySQL query is:

SELECT something FROM table WHERE column REGEXP 'regexp'

For example, to select all columns from the table events where the values in the column id end with 5587, use:

SELECT * FROM events WHERE id REGEXP '5587$'

A more elaborate example selects all columns of the table reviews where the values in the column description contain the word excellent:

SELECT * FROM reviews WHERE description REGEXP '[[:<:]]excellent[[:>:]]'

Comments Anyone?


Joe Taylor
In CodeIgniter using Active Record, the syntax would be:
$this->db->where('field_name REGEXP', '[[:<:]]string[[:>:]]');

Add YOUR two cents worth

Add your comments praise for the things we say and do here!

Your Information
Your Comment
Submit