MySQL database - MySQL LIKE: fuzzy query

In MySQL, the LIKE keyword is mainly used to search for specified content in the matching field. Its syntax format is as follows:

[NOT] LIKE 'string'

in:

  • NOT : optional parameter, the condition is met when the content in the field does not match the specified string.
  • String: Specify the string to match. "String" can be a very complete string, or it can contain wildcards.

The LIKE keyword supports the percent sign "%" and underscore "_" wildcards.

Wildcard is a special statement, mainly used for fuzzy query. When you don't know the real characters or are too lazy to enter the full name, you can use wildcards to replace one or more real characters. 

Queries with "%" wildcard

"%" is the most commonly used wildcard in MySQL, it can represent a string of any length, and the length of the string can be 0 . For example, a%brepresents a string of any length starting with the letter a and ending with the letter b. This string can represent strings such as ab, acb, accb, accrb, etc.

Example 1

In the tb_students_info table, find all the student names starting with the letter "T", the SQL statement and the running results are as follows:

mysql> SELECT name FROM tb_students_info
    -> WHERE name LIKE 'T%';
+--------+
| name   |
+--------+
| Thomas |
| Tom    |
+--------+
2 rows in set (0.12 sec)

As you can see, only student names starting with the letter "T" are returned in the query results.

Note: Matched strings must be enclosed in single or double quotes.

NOT LIKE means the condition is met if the strings do not match.

Example 2

In the tb_students_info table, find all the student names that do not start with the letter "T". The SQL statement and running results are as follows:

mysql> SELECT NAME FROM tb_students_info
    -> WHERE NAME NOT LIKE 'T%';
+-------+
| NAME  |
+-------+
| Dany  |
| Green |
| Henry |
| Jane  |
| Jim   |
| John  |
| Lily  |
| Susan |
+-------+
8 rows in set (0.00 sec)

As you can see, the query results return student names that do not begin with the letter "T".

Example 3

In the tb_students_info table, find all the student names containing the letter "e", the SQL statement and the running results are as follows:

mysql> SELECT name FROM tb_students_info
    -> WHERE name LIKE '%e%';
+-------+
| name  |
+-------+
| Green |
| Henry |
| Jane  |
+-------+
3 rows in set (0.00 sec)

As you can see, all student names containing the letter "e" are returned in the query results.

Queries with "_" wildcards

"_" can only represent a single character, and the length of a character cannot be 0. For example, a_bit can represent strings such as acb, adb, aub, etc.

Example 4

In the tb_students_info table, find all student names that end with the letter "y" and have only 4 letters before "y". The SQL statement and running results are as follows:

mysql> SELECT name FROM tb_students_info
    -> WHERE name LIKE '____y';
+-------+
| name  |
+-------+
| Henry |
+-------+
1 row in set (0.00 sec)

LIKE is case sensitive

By default, the LIKE keyword matches characters case-insensitively. If you need case sensitivity, you can add the BINARY keyword.

Example 5

In the tb_students_info table, find all student names starting with the letter "t". The case-sensitive and case-insensitive SQL statements and running results are as follows:

mysql> SELECT name FROM tb_students_info WHERE name LIKE 't%';
+--------+
| name   |
+--------+
| Thomas |
| Tom    |
+--------+
2 rows in set (0.00 sec)

mysql> SELECT name FROM tb_students_info WHERE name LIKE BINARY 't%';
Empty set (0.01 sec)

It can be seen from the results that after case-sensitive, records such as "Tom" and "Thomas" will not be matched.

Notes and Tips for Using Wildcards

Here are some considerations for using wildcards:

  • Pay attention to capitalization . MySQL is case insensitive by default. If case sensitive, data like "Tom" cannot be matched by "t%".
  • Note the trailing spaces , which interfere with wildcard matching. For example, "T%" would not match "Tom".
  • Note the NULL . The "%" wildcard can match any character, but cannot match NULL. That is to say, "%" cannot match the record whose value is NULL in the tb_students_info data table.

Here are some tips to keep in mind when using wildcards.

  • Do not overuse wildcards, other operators should be used if they serve the same purpose. Because MySQL generally takes longer to process wildcards than other operators.
  • Once you've decided to use wildcards, don't use them at the beginning of a string unless absolutely necessary. Put the wildcard at the beginning of the search pattern, the slowest to search.
  • Pay careful attention to the placement of the wildcards. If misplaced, it may not return the desired data.

In short, wildcards are an extremely important and useful search tool, and we will use it frequently in the future.

expand

If the query contains wildcards, you can use the "\" escape character. For example, in the tb_students_info table, after changing the student name "Dany" to "Dany%", query the student names ending with "%", the SQL statement and the running results are as follows:

mysql> SELECT NAME FROM test.`tb_students_info` WHERE NAME LIKE '%\%';
+-------+
| NAME  |
+-------+
| Dany% |
+-------+
1 row in set (0.00 sec)

Dark horse programmer MySQL database entry to proficiency, from mysql installation to mysql advanced, mysql optimization all covered

Guess you like

Origin blog.csdn.net/Itmastergo/article/details/130359453
Recommended