String Matching in MySQL

Source: Internet
Author: User

Abstract: This article describes the knowledge about string pattern matching. Standard SQL mode matching is the standard of SQL language and can be accepted by other relational database systems. Extended regular expression pattern matching is developed according to the standard of the Unix system. It can only be used in MySQL, but its function is stronger than the standard SQL pattern matching.

MySQL provides standard SQL mode matching and an extended regular expression mode matching format based on Unix utilities such as vi, grep, and sed.

Standard SQL mode matching

SQL pattern matching allows you to use "_" to match any single character, while "%" matches any number of characters (including zero characters ). In MySQL, the SQL mode is case-insensitive by default. Some examples are shown below. Note that when you use SQL mode, you cannot use = or! =; And use the LIKE or not like comparison operator.

For example, in the pet table, to find the name starting with "B:

Mysql> SELECT * FROM pet WHERE name LIKE "B % ";


+ -------- + --------- + ------ + ------------ +

| Name | owner | species | sex | birth | death |

+ -------- + --------- + ------ + ------------ +

| Buffy | Harold | dog | f | 1989-05-13 | NULL |

| Boane | Diane | dog | m |

+ -------- + --------- + ------ + ------------ +

To find the name ending with "fy:

Mysql> SELECT * FROM pet WHERE name LIKE "% fy ";


+ -------- + --------- + ------ + ------------ + ------- +

| Name | owner | species | sex | birth | death |

+ -------- + --------- + ------ + ------------ + ------- +

| Fluffy | Harold | cat | f | 1993-02-04 | NULL |

| Buffy | Harold | dog | f | 1989-05-13 | NULL |

+ -------- + --------- + ------ + ------------ + ------- +

To find the name containing "w:

Mysql> SELECT * FROM pet WHERE name LIKE "% w % ";


+ ---------- + ------- + --------- + ------ + ------------ +

| Name | owner | species | sex | birth | death |

+ ---------- + ------- + --------- + ------ + ------------ +

| Claws | Gwen | cat | m | 1994-03-17 | NULL |

| Boane | Diane | dog | m |

| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |

+ ---------- + ------- + --------- + ------ + ------------ +

To locate the name containing exactly five characters, use the "_" pattern character:

Mysql> SELECT * FROM pet WHERE name LIKE "_____";


+ ------- + -------- + --------- + ------ + ------------ + ------- +

| Name | owner | species | sex | birth | death |

+ ------- + -------- + --------- + ------ + ------------ + ------- +

| Claws | Gwen | cat | m | 1994-03-17 | NULL |

| Buffy | Harold | dog | f | 1989-05-13 | NULL |

+ ------- + -------- + --------- + ------ + ------------ + ------- +

Extended Regular Expression Pattern Matching

Other types of pattern matching provided by MySQL use extended regular expressions. Use the REGEXP and not regexp operators (or RLIKE and NOT

RLIKE, they are synonyms ).

Some Characters of the extended regular expression are:

"." Matches any single character.

A character class "[...]" matches any character in square brackets. For example, "[abc]" matches "a", "B", or "c ". To name a range of characters, use a hyphen (-).

"[A-z]" matches any lowercase letter, and "[0-9]" matches any number.

"*" Matches zero or multiple items before it. For example, "x *" matches any number of "x" characters, "[0-9] *" matches any number of numbers, and ". *" matches any number.

Anything.

Regular Expressions are case-sensitive, but if you want to, you can use one character class matching method. For example, "[aA]" matches lowercase or upper-case "a" and "[a-zA-Z ]"

Any letter in either of the two statements.

If it appears anywhere in the tested value, the schema matches (as long as they match the entire value, the SQL schema matches ).

To locate a pattern so that it must match the start or end of the tested value, use "^" at the start of the pattern or "$" at the end of the pattern ".

To demonstrate how the extended regular expression works, the LIKE Query shown above is rewritten using REGEXP below:

To find the name starting with "B", use "^" to match the start of the name and use "[bB]" to match "B" in lower case or upper case ":

Mysql> SELECT * FROM pet WHERE name REGEXP "^ [bB]";


+ -------- + --------- + ------ + ------------ +

| Name | owner | species | sex | birth | death |

+ -------- + --------- + ------ + ------------ +

| Buffy | Harold | dog | f | 1989-05-13 | NULL |

| Boane | Diane | dog | m |

+ -------- + --------- + ------ + ------------ +

To find the name ending with "fy", use "$" to match the end Of the name:

Mysql> SELECT * FROM pet WHERE name REGEXP "fy $ ";


+ -------- + --------- + ------ + ------------ + ------- +

| Name | owner | species | sex | birth | death |

+ -------- + --------- + ------ + ------------ + ------- +

| Fluffy | Harold | cat | f | 1993-02-04 | NULL |

| Buffy | Harold | dog | f | 1989-05-13 | NULL |

+ -------- + --------- + ------ + ------------ + ------- +

To locate the name containing a "w", use "[wW]" to match the "w" in lower case or upper case ":

Mysql> SELECT * FROM pet WHERE name REGEXP "[wW]";


+ ---------- + ------- + --------- + ------ + ------------ +

| Name | owner | species | sex | birth | death |

+ ---------- + ------- + --------- + ------ + ------------ +

| Claws | Gwen | cat | m | 1994-03-17 | NULL |

| Boane | Diane | dog | m |

| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |

+ ---------- + ------- + --------- + ------ + ------------ +

Since a regular expression appears anywhere in the value and its pattern matches, there is no need to place a wildcard in the two aspects of the pattern in the previous query so that it matches the entire value,

Just like if you use an SQL mode.

To locate a name that contains exactly five characters, use "^" and "$" to match the start and end of the name, and the five "." instances are in the range:

Mysql> SELECT * FROM pet WHERE name REGEXP "^... $ ";


+ ------- + -------- + --------- + ------ + ------------ + ------- +

| Name | owner | species | sex | birth | death |

+ ------- + -------- + --------- + ------ + ------------ + ------- +

| Claws | Gwen | cat | m | 1994-03-17 | NULL |

| Buffy | Harold | dog | f | 1989-05-13 | NULL |

+ ------- + -------- + --------- + ------ + ------------ + ------- +

You can also use the "{n}" "Repeat n times" operator to rewrite the previous query:

Mysql> SELECT * FROM pet WHERE name REGEXP "^. {5} $ ";


+ ------- + -------- + --------- + ------ + ------------ + ------- +

| Name | owner | species | sex | birth | death |

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.