SQL Getting Sta...login
SQL Getting Started Tutorial Manual
author:php.cn  update time:2022-04-12 14:15:40

SQL LIKE



The LIKE operator is used to search for a specified pattern in a column in the WHERE clause.


SQL LIKE Operator

The LIKE operator is used to search for a specified pattern in a column in the WHERE clause.

SQL LIKE syntax

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;


Demo Database

In this tutorial, we will use the php sample database.

The following is the data selected from the "Websites" table:

+----+--------------+--- ------------------------+------+---------+
| id | name | url                                                                    --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | php Chinese website | http://www.php.cn/ | 4689 | CN |
| 4 | Weibo | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+----------- ----------------+-------+---------+

SQL LIKE operation Example

The following SQL statement selects all customers whose name starts with the letter "G":

Example

SELECT * FROM Websites
WHERE name LIKE 'G%';
Execution output result:

Tip: The "%" symbol is used to define wildcard characters (default letters) before and after the pattern. You'll learn more about wildcards in the next chapter.

The following SQL statement selects all customers whose name ends with the letter "k":

Example

SELECT * FROM Websites
WHERE name LIKE '% k';

Execution output result:

The following SQL statement selects all customers whose name contains the pattern "oo":

Example

SELECT * FROM Websites
WHERE name LIKE '%oo%';

Execution output result:

By using the NOT keyword, you can select records that do not match the pattern.

The following SQL statement selects all customers whose name does not contain the pattern "oo":

Example

SELECT * FROM Websites
WHERE name NOT LIKE ' %oo%';

Execution output result: