MySQL LIKE and Wildcards
LIKE compares text against a pattern. Two wildcard characters make patterns flexible:
| Wildcard | Matches | Example | Matches |
|---|---|---|---|
% |
any number of characters (including none) | 'S%' |
Sourdough Loaf, Salted Butter 250g |
_ |
exactly one character | '_e%' |
names whose second letter is e |
SELECT name FROM products WHERE name LIKE 'S%';+----------------------+
| name |
+----------------------+
| Sparkling Water 6x1L |
| Sourdough Loaf |
| Salted Butter 250g |
| Sea Salt Crisps |
+----------------------+
Common patterns
| Pattern | Meaning |
|---|---|
'abc%' |
starts with abc |
'%abc' |
ends with abc |
'%abc%' |
contains abc anywhere |
'a%z' |
starts with a and ends with z |
'___' |
exactly three characters |
'_r%' |
r in second position |
SELECT name FROM products WHERE name LIKE '%250g';
SELECT name FROM products WHERE name LIKE '%choc%';
SELECT first_name FROM customers WHERE first_name LIKE '___';+--------------------+
| name |
+--------------------+
| Aged Gruyere 250g |
| Salted Butter 250g |
+--------------------+
+--------------------+
| name |
+--------------------+
| Chocolate Cake |
| Dark Chocolate Bar |
+--------------------+
+------------+
| first_name |
+------------+
| Ana |
| Ben |
| Mia |
+------------+
The '%choc%' search found "Dark Chocolate Bar" and "Chocolate Cake" despite the capital C: like =, LIKE is case-insensitive with the default collation.
NOT LIKE
SELECT name FROM products WHERE category_id = 3 AND name NOT LIKE '%250g';
SELECT SUM(email LIKE '%@example.com') AS matching,
SUM(email NOT LIKE '%@example.com') AS not_matching,
SUM(email IS NULL) AS null_emails
FROM customers;+-------------------+
| name |
+-------------------+
| Whole Milk 1L |
| Greek Yogurt 500g |
+-------------------+
+----------+--------------+-------------+
| matching | not_matching | null_emails |
+----------+--------------+-------------+
| 12 | 0 | 3 |
+----------+--------------+-------------+
The second query adds up conditions (each is 1, 0 or NULL) to count them. Twelve e-mails match the pattern, none fail it, and the three NULL e-mails are in neither group: NULL LIKE ... and NULL NOT LIKE ... are both NULL. A NULL never matches any pattern, positive or negative.
Matching a literal % or _
To search for an actual percent sign or underscore, escape it with a backslash (\%, \_), or pick your own escape character with ESCAPE:
SELECT 'sale_50%' LIKE '%\_50\%' AS with_backslash,
'sale_50%' LIKE '%!_50!%' ESCAPE '!' AS with_escape,
'sale-50%' LIKE '%\_50%' AS dash_is_not_underscore;+----------------+-------------+------------------------+
| with_backslash | with_escape | dash_is_not_underscore |
+----------------+-------------+------------------------+
| 1 | 1 | 0 |
+----------------+-------------+------------------------+
Regular expressions
When % and _ aren't enough, REGEXP (also spelled RLIKE) matches a regular expression. Unlike LIKE, a regular expression matches anywhere in the string unless you anchor it with ^ and $:
SELECT name FROM products WHERE name REGEXP '[0-9]+(kg|g|L)