MySQL LIKE
In MySQL, the LIKE condition filters the results obtained through the SELECT, INSERT, UPDATE, and DELETE statements based on pattern matching.
Syntax:
WHERE expressions LIKE pattern [ ESCAPE 'escape_character' ]
Parameters:
expressions: It is used to specify a column or a field.
pattern: It is used to specify the character expression for pattern matching.
escape_character: It is an optional parameter that is used to test for literal instances of a wildcard character such as % or _. Its default value is “\”.
Example: Using wildcard: Percent(%). Items table:
ID | NAME | QUANTITY |
1 | Electronics | 30 |
2 | Sports | 45 |
3 | Fashion | 100 |
4 | Grocery | 90 |
5 | Toys | 50 |
Query:
SELECT name FROM items WHERE name LIKE 'Elec%';
Output:
NAME Electronics
Example: Using wildcard: Underscore(_). Items table:
ID | NAME | QUANTITY |
1 | Electronics | 30 |
2 | Sports | 45 |
3 | Fashion | 100 |
4 | Grocery | 90 |
5 | Toys | 50 |
Query:
SELECT name FROM items WHERE name LIKE 'Elec__onics';
Output:
NAME Electronics
Example: Using NOT. Items table:
ID | NAME | QUANTITY |
1 | Electronics | 30 |
2 | Sports | 45 |
3 | Fashion | 100 |
4 | Grocery | 90 |
5 | Toys | 50 |
Query:
SELECT name FROM items WHERE name NOT LIKE 'Elec%';
Output:
NAME Electronics