Code Snippets » Regular Expressions

Use regular expressions in MySQL SELECT statements



A very cool and powerful capability in MySQL and other databases is the ability to incorporate regular expression syntax when selecting data. The regular expresion support in MySQL is extensive. This article reviews regular expression use in MySQL and lists the supported regular expression metacharacters.

The basic syntax to use regular expressions in a MySQL query is:



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



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



MySQL allows the following regular expression metacharacters:



MySQL interprets a backslash () character as an escape character. To use a backslash in a regular expression, you must escape it with another backslash (\).

Thanks goes out to tech-recipes.com for this very useful article.