TExcelTAD
Home › MySQL › MySQL LIKE and Wildcards

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
mysql · try it yourself
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
mysql · try it yourself
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

mysql · try it yourself
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:

mysql · try it yourself
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 $:

mysql · try it yourself
SELECT name FROM products WHERE name REGEXP '[0-9]+(kg|g|L)
  

;
+----------------------+
| name                 |
+----------------------+
| Espresso Beans 1kg   |
| Orange Juice 1L      |
| Sparkling Water 6x1L |
| Whole Milk 1L        |
| Aged Gruyere 250g    |
| Greek Yogurt 500g    |
| Salted Butter 250g   |
| Gala Apples 1kg      |
| Cherry Tomatoes 500g |
| Roasted Almonds 200g |
+----------------------+

That finds products whose name ends with a size such as 1kg, 500g or 1L.

Tip: A pattern starting with % ('%choc%') can't use an ordinary index, so on a big table it reads every row. For real text search, MySQL offers FULLTEXT indexes with MATCH ... AGAINST.

Test yourself

Which pattern matches names that END with 'Loaf'?

  1. 'Loaf%'
  2. '%Loaf'
  3. '_Loaf'
  4. '%Loaf%'

How many characters does _ match?

  1. Zero or more
  2. Exactly one
  3. One or more
  4. Zero or one

Does name LIKE '%tea%' match 'Earl Grey Tea' with the default collation?

  1. Yes
  2. No, LIKE is case-sensitive
  3. Only with REGEXP
  4. Only if the column is BINARY