MySQL fuzzy query syntax

Source: Internet
Author: User

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

Format of the pattern match.

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 the SQL mode, you do not

Can use = or! =; And use the LIKE or not like comparison operator.

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 |
+ ------- + -------- + --------- + ------ + ------------ + ------- +

Other types of pattern matching provided by MySQL use extended regular expressions. Use

REGEXP and not regexp operators (or RLIKE and not rlike, which 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
What is the number.
"*" Matches zero or multiple items before it. For example, "x *" matches any number of "x" characters and "[0-9] *"
Match any number of digits, and ". *" matches any number of things.
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]" matches 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 beginning of the pattern or
End with "$ ".
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 look at the two

Put a wildcard to match the entire value, just as if you use an SQL mode.

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

Between the two:

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 |
+ ------- + -------- + --------- + ------ + ------------ + ------- +
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+ ------- + -------- + --------- + ------ + ------------ + ------- +

Search for numbers and other fuzzy query statements
Select * from pet where name REGEXP "[^ a-zA-Z].";

 

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.