Use LIKE fuzzy query

String matching syntax is as follows:

<Expression 1> [NOT] LIKE <Expression 2>

Pattern matching is a matching string, LIKE operator is provided the use of a filter to be a wildcard match operation condition, it determines whether or not equal compared.

Object matching operation between each other may be CHAR, VARCHAR, TEXT, DATETIME data types. Results operation returns TRUE or FALSE.

After using the wildcard search pattern can create a relatively specific data in determining the comparison value is not entirely the case, and placed keyword LIKE. Wildcards can be used anywhere in the search mode, and may use multiple wildcards. MySQL supports wildcards There are two:

1) percent (%)

Percent sign is commonly used as a wildcard in MySQL, in the filter, the percent sign may represent any character string, and the string can appear any number of times.

Use percent wildcard to note the following:

  • MySQL default is not case sensitive to case-sensitive, you need to replace the character set collation.
  • Percent sign does not match a null value.
  • Percent search mode may represent 0, 1 or more characters in a given position.
  • Trailing spaces may interfere with wildcard matching, can generally be attached to the end of a percent sign in a search mode.

2) an underscore (_)

Uses wildcards and underlined percent wildcard, like underline match only a single character, rather than a plurality of characters, the character is not 0.

Note: Do not overuse wildcards, the handling of general wildcard search takes longer than other search methods.

[Example 4] In tb_students_info table, find the names of all the students to "T" starting with the letter, SQL statements and input the results shown below.

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

Note: When searching for matching the wildcard "%" can be placed in different locations.

[Example 5] In tb_students_info table, find all student name contains "e" letter, the SQL statements and input the execution result is shown below.

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

Can be seen from the results, the statement contained in the query string student's e-letter name, as long as the letter e names, their front or back no matter how many characters are to meet the conditions of the query.

[Example 6] In tb_students_info table, look for the end of all the letter "y", and "y" of students in front of only four letters of the name, SQL statements, and input the results shown below.

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

Guess you like

Origin www.cnblogs.com/lvchengda/p/12532168.html