Wildcard filtering-LIKE,%, _

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:

  1. MYSQL wildcards are useful, but this feature comes at a price, and the search efficiency of wildcards takes longer than other searches
  2. Pay attention to the position of the wildcard, if you misplace it, it may not return the desired data
  3. 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
  4. NULL cannot be matched with wildcards
Published 138 original articles · praised 34 · 150,000 views

Guess you like

Origin blog.csdn.net/bbj12345678/article/details/105469443