In MySQL, the LIKE operator is used to search for a specified pattern for a column in a WHERE clause .
LIKE Syntax
SELECT column_name
FROM table_name
WHERE column_name LIKE pattern;
We have a table "employee" as below:
employee
id first_name salary country
.......................................................
1 John 10000 Canada
2 Chris 20000 California
3 Max 30000 London
4 Jenny 25000 Canada
LIKE Operator Examples
The below statement selects all employee with "country" starting with the "c" alphabet in the "employee" table:
SELECT * FROM employee
WHERE country LIKE 'c%';
Result
id first_name salary country
.......................................................
1 John 10000 Canada
2 Chris 20000 California
4 Jenny 25000 Canada
The below statement selects all employee with "country" ending with the "n" alphabet in the "employee" table:
SELECT * FROM employee
WHERE country LIKE '%n';
Result
id first_name salary country
.......................................................
3 Max 30000 London
The below statement selects all employee with "country" containing pattern "can" in the "employee" table:
SELECT * FROM employee
WHERE country LIKE '%can%';
Result
id first_name salary country
.......................................................
1 John 10000 Canada
4 Jenny 25000 Canada
To get the records that do not match with the specified pattern we use NOT keyword as below:
SELECT * FROM employee
WHERE country NOT LIKE '%can%';
Result
id first_name salary country
.......................................................
2 Chris 20000 California
3 Max 30000 London
Hope this will help you :)
0 Comment(s)