
Regular Expressions in MySQL
A very interesting and useful capability of MySQL is to incorporate Regular Expressions (regex) in SQL queries. The regular expression support in MySQL is extensive. Let's take a look at using Regular Expressions in queries and the supported operators.
Using Regular Expressions in queries
A simple example of using Regular Expressions in a SQL query would be to select all names from a table that start with 'A'.
A slight modification in the above example to look for names starting with 'A' or 'D' or 'F' will look like this.
If we want to select all names ending with 'P', then the SQL query goes like this
Regular Expression Operators
MySQL interprets a backslash () character as an escape character. To use a backslash in a regular expression, you must escape it with another backslash ().
Whether the Regular Expression match is case sensitive or otherwise is decided by the collation method of the table. If your collation method name ends with ci then the comparison/match is case-insensitive, else if it end in cs then the match is case sensitive.
Other Examples
Checking only for numbers
Contains a specific word, for example the skill PHP in skill sets
Fetching records where employees have entered their 10-digit mobile number as the contact number.
