When using the query, we often encounter fuzzy condition queries, and fuzzy query involves string pattern matching.
Here, we will mainly talk about two aspects: Standard SQL pattern matching and extended regular expression pattern matching.
I. matching the standard SQL Mode
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 using SQL Mode, you cannot use = OR! =; And use the LIKE or not like comparison operator.
To find the name that contains exactly five characters, use the "_" pattern character to write it like this:
SELECT * FROM student WHERE name LIKE "_"; (spaces are not available, just for ease of demonstration)
"%" Matches the same as oracle, which is omitted here.
II. Extended Regular expression pattern matching
Similar to Java or javascript regular expressions.
Other types of pattern matching provided by MySQL use extended regular expressions. When you perform a match test on this type of pattern, use the REGEXP and not regexp operators (or RLIKE and not rlike, they are
Synonym ).
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 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 start of the pattern or "$" at the end of the pattern ".
The following is a simple example:
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 student 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 student 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 student 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 student 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 student 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 |
+ ------- + -------- + --------- + ------ + ------------ + ------- +