Database Reference
In-Depth Information
Another useful wildcard is the underscore (
_
). The underscore is used just like
%
, but instead of matching multiple characters, the underscore matches just a
single character.
Take a look at this example:
▼
Input
SELECT prod_id, prod_name
FROM products
WHERE prod_name LIKE '_ ton anvil';
▼
Output
+---------+-------------+
| prod_id | prod_name |
+---------+-------------+
| ANV02 | 1 ton anvil |
| ANV03 | 2 ton anvil |
+---------+-------------+
▼
Analysis
The search pattern used in this
WHERE
clause specifies a wildcard followed by
literal text. The results shown are the only rows that match the search pattern:
The underscore matches
1
in the first row and
2
in the second row. The
.5
ton anvil
product did not match because the search pattern matched a single
character, not two. By contrast, the following
SELECT
statement uses the
%
wildcard and returns three matching products:
▼
Input
SELECT prod_id, prod_name
FROM products
WHERE prod_name LIKE '% ton anvil';
▼
Output
+---------+--------------+
| prod_id | prod_name |
+---------+--------------+
| ANV01 | .5 ton anvil |
| ANV02 | 1 ton anvil |
| ANV03 | 2 ton anvil |
+---------+--------------+
Unlike
%
, which can match zero characters,
_
always matches one character—
no more and no less.