Languages
[Edit]
EN

MySQL - LIKE operator

3 points
Created by:
maxsior322
347

In this article, we would like to show you how to use LIKE operator in MySQL.

Quick solution:

 

We use LIKE operator in a WHERE clause to find rows that contan column cell that matches indicated pattern.

There are two wildcards used with the LIKE operator:

  • % - matches 0 or more characters (0, 1, 2, 3, ...),
  • _ - matches any single character (requres in _ place some character).
LIKE operatorDescription
Finds any rows that name cell starts with prefix text.
Finds any rows that name cell ends with postfix text.
Finds any rows that name cell has middle text somewhere inside.

Finds any rows that name cell:

  • starts with part1 text
  • and has part2 text somewhere inside
  • and ends with part3 text.

 

Practical example

Edit

To understand LIKE operator patterns better way, look at the below examples:

To show how the LIKE operator works, we will use the following table:

MySQL - example data used with LIKE operator
MySQL - example data used with LIKE operator

Note:

At the end of this article you can find database preparation SQL queries.

Example

Edit

In this example, we will display Jonh user row using some LIKE operator cases taken from above patterns. 

Queries:

Result:

MySQL - LIKE operator result
MySQL - LIKE operator result

Database preparation

Edit

Use below queries to prepare database and test LIKE operator.

create_tables.sql file:

insert_data.sql file:

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MySQL

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join