- MySQL - Database Import
- MySQL - Database Export
- MySQL - SQL Injection
- MySQL - Handling Duplicates
- MySQL - Using Sequences
- MySQL - Database Info
- MySQL - Clone Tables
- MySQL - Temporary Tables
- MySQL - Indexes
- MySQL - Alter Command
- MySQL - Transactions
- MySQL - Regexps
- MySQL - NULL Values
- MySQL - Using Join
- MySQL - Sorting Results
- MySQL - Like Clause
- MySQL - Delete Query
- MySQL - Update Query
- MySQL - Where Clause
- MySQL - Select Query
- MySQL - Insert Query
- MySQL - Drop Tables
- MySQL - Create Tables
- MySQL - Data Types
- MySQL - Select Database
- MySQL - Drop Database
- MySQL - Create Database
- MySQL - Connection
- MySQL - PHP Syntax
- MySQL - Administration
- MySQL - Installation
- MySQL - Introduction
- MySQL - Home
MySQL Useful Resources
- MySQL - Discussion
- MySQL - Useful Resources
- MySQL - Quick Guide
- MySQL - Statements Reference
- MySQL - Useful Functions
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MySQL - Regexps
You have seen MySQL pattern matching with LIKE ...%. MySQL supports another type of pattern matching operation based on the regular expressions and the REGEXP operator. If you are aware of PHP or PERL, then it is very simple for you to understand because this matching is same pke those scripting the regular expressions.
Following is the table of pattern, which can be used along with the REGEXP operator.
Pattern | What the pattern matches |
---|---|
^ | Beginning of string |
$ | End of string |
. | Any single character |
[...] | Any character psted between the square brackets |
[^...] | Any character not psted 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 am psting a few for your understanding.
Consider we have a table called person_tbl and it is having a field called name −
Query to find all the names starting with st −
mysql> SELECT name FROM person_tbl WHERE name REGEXP ^st ;
Query to find all the names ending with ok −
mysql> SELECT name FROM person_tbl WHERE name REGEXP ok$ ;
Query to find all the names, which contain mar −
mysql> SELECT name FROM person_tbl WHERE name REGEXP mar ;
Query to find all the names starting with a vowel and ending with ok −
mysql> SELECT FirstName FROM intque.person_tbl WHERE FirstName REGEXP ^[aeiou].*ok$ ;Advertisements