MySQL LIKE
The LIKE operator is a logical operator used in WHERE clause that tests whether a string contains a specified pattern or not.
Two wildcards are often used with the LIKE operator:
- %: represents zero, one, or more characters.
- _ : represents a single character.
Syntax:
SELECT column1, column2, ..., column_n FROM tableX WHERE columnX LIKE pattern;
Let’s take a few examples of using the LIKE operator to see how it works. For this we will use the “Clients” table.
1- The following query displays all values beginning with “a”:
SELECT * FROM Clients WHERE Name LIKE 'a%';
2- The following query displays values that end with “t”:
SELECT * FROM Clients WHERE Name LIKE '%t';
[st_adsense]
3- The following query displays all values that have “li” in any position:
SELECT * FROM Clients WHERE Name LIKE '%li%';
4- The following query displays all values that have “o” in the second position:
SELECT * FROM Clients WHERE Name LIKE '_o%';
5- The following query displays all values starting with “a” and having at least 3 characters:
SELECT * FROM Clients WHERE Name LIKE 'a__%';
6- The following query displays all values starting with “b” and ending with “b”:
SELECT * FROM Clients WHERE Name LIKE 'b%b';
[st_adsense]