Previous: [MySQL Must Know and Know (4)] [Data Filtering]
+++++++++++++Start line++++++++++++++++
Article Directory
One, the LIKE operator
Wildcard: a special character used to match part of a value
Search mode: search conditions composed of literal values, wildcards, or a combination of both
The wildcard itself is actually a character with special meaning in the WHERE clause of SQL
1.1 Percent sign wildcard
The most commonly used wildcard is the percent sign. In the search string,% means any character appears any number of times
mysql> SELECT prod_id, prod_name
-> FROM products
-> WHERE prod_name LIKE 'jet%';
Case sensitive
Depending on how MySQL is configured, searches can be case sensitive. 'jet%' and JetPack 1000 will not match
Wildcards can be used anywhere in the search pattern
mysql> SELECT prod_id, prod_name
-> FROM products
-> WHERE prod_name LIKE '%anvil%';
Wildcards can also appear in the middle of the search pattern
mysql> SELECT prod_name
-> FROM products
-> WHERE prod_name LIKE 's%e';
Pay attention to trailing spaces.
Trailing spaces may interfere with wildcard matching. A good way is to use functions
Note that NULL
seems to match anything with% wildcards, but there are exceptions, namely NULL
1.2 Underscore wildcard
The underscore value matches a single character instead of multiple characters
mysql> SELECT prod_id, prod_name
-> FROM products
-> WHERE prod_name LIKE '_ ton anvil';
mysql> SELECT prod_id, prod_name
-> FROM products
-> WHERE prod_name LIKE '% ton anvil';
2. Skills of using wildcards
1. Don't overuse wildcards
2. When you really need to use wildcards, don't use them at the beginning of the search pattern unless necessary
3. Pay careful attention to the position of wildcards
+++++++++++++End line++++++++++++++++
Next: [MySQL must know and know (6)] [Search with regular expressions]