
- MySQLi Tutorial
- MySQLi - Home
- MySQLi - Introduction
- MySQLi - PHP Syntax
- MySQLi - Connection
- MySQLi - Create Database
- MySQLi - Drop Database
- MySQLi - Select Database
- MySQLi - Create Tables
- MySQLi - Drop Tables
- MySQLi - Insert Query
- MySQLi - Select Query
- MySQLi - Where Clause
- MySQLi - Update Query
- MySQLi - Delete Query
- MySQLi - Like Clause
- MySQLi - Sorting Results
- MySQLi - Using Joins
- MySQLi - Handling NULL Values
- Obtaining & Using MySQLi Metadata
- MySQL
- MySQL - Installation
- MySQL - Administration
- MySQL - Data Types
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQLi Useful Resources
- MySQLi - Useful Functions
- MySQLi - Quick Guide
- MySQLi - Useful Resources
- MySQLi - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQLi - Regexps
You have seen MySQL pattern matching with LIKE ...%. MySQL supports another type of pattern matching operation based on regular expressions and the REGEXP operator. If you are aware of PHP or PERL, then it's very simple for you to understand because this matching is very similar to those scripting regular expressions.
Following is the table of pattern, which can be used along with REGEXP operator.
Pattern | What the pattern matches |
---|---|
^ | Beginning of string |
$ | End of string |
. | Any single character |
[...] | Any character listed between the square brackets |
[^...] | Any character not listed between the square brackets |
p1|p2|p3 | Alternation; matches any of the patterns p1, p2, or p3 |
* | Zero or more instances of preceding element |
+ | One or more instances of preceding element |
{n} | n instances of preceding element |
{m,n} | m through n instances of preceding element |
Examples
Now based on above table, you can device various type of SQL queries to meet your requirements. Here, I'm listing few for your understanding. Consider we have a table called tutorials_inf and it's having a field called name −
Query to find all the names starting with 'sa'
mysql> SELECT * FROM tutorials_inf WHERE name REGEXP '^sa';
The sample output should be like this −
+----+------+ | id | name | +----+------+ | 1 | sai | +----+------+ 1 row in set (0.00 sec)
Query to find all the names ending with 'ai'
mysql> SELECT * FROM tutorials_inf WHERE name REGEXP 'ai$';
The sample output should be like this −
+----+------+ | id | name | +----+------+ | 1 | sai | +----+------+ 1 row in set (0.00 sec)
Query to find all the names, which contain 'a'
mysql> SELECT * FROM tutorials_inf WHERE name REGEXP 'a';
The sample output should be like this −
+----+-------+ | id | name | +----+-------+ | 1 | sai | | 3 | ram | | 4 | johar | +----+-------+ 3 rows in set (0.00 sec)
Query to find all the names starting with a vowel
mysql> SELECT * FROM tutorials_inf WHERE name REGEXP '^[aeiou]';