CodesJava

Easy learning with example program codes

Like clause in MySQL

MySQL LIKE
In MySQL, the LIKE condition is used to filter 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 which 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
1 row  in set <0.00 sec>

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
1 row  in set <0.00 sec>

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
1 row  in set <0.00 sec>
Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status