All the operators introduced earlier are filtered for known values, but this filtering method is not always useful. Use wildcards to create search patterns for special data
Wildcards: special characters used to match part of the value, in order to use wildcards, you must use the LIKE operator
Search mode: search conditions consisting of literal values, wildcards, or a combination of both
- % Wildcard (The most commonly used wildcard is the percent sign%. In the search string,% means that any character appears any number of times)
- For example, to retrieve the name field, words starting with small
SELECT * FROM user WHERE name LIKE '小%'
- Wildcards can be used anywhere in the search pattern. Below are two wildcards, which are located at both ends of the pattern
- Please note that% matches 0 to more characters
SELECT * FROM user WHERE name LIKE '%飞%'
- _ Wildcard (underscore _ wildcard uses the same as%, but it only matches one character instead of multiple)
SELECT * FROM user WHERE name LIKE '小_'
to sum up:
- MYSQL wildcards are useful, but this feature comes at a price, and the search efficiency of wildcards takes longer than other searches
- Pay attention to the position of the wildcard, if you misplace it, it may not return the desired data
- Trailing spaces may interfere with wildcard matching. For example, if there is one or more spaces after fly,% fly will not match them. To solve this problem, you can use% fly% to match, or use functions to remove the first and last spaces
- NULL cannot be matched with wildcards