MySQL LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
MySQL LIKE uses % and _..
SELECT column1, column2, ... FROM table_name WHERE columnN LIKE pattern;
Let's put these statements into real use.
We've a table named Students in our database that contains the following records:
Now we will fetch records from the Students table with a NAME starting with "s".
SELECT * FROM Students WHERE NAME LIKE '%s';
The above SELECT statement selects NAME starts with s alphabet Students table.
Examples showing different LIKE operators with '%' and '_' wildcards:
|WHERE NAME LIKE 's%'||Finds any values that start with "s"|
|WHERE NAME LIKE '%s'||Finds any values that end with "s"|
|WHERE NAME LIKE '%or%'||Finds any values that have "or" in any position|
|WHERE NAME LIKE '_s%'||Finds any values that have "s" in the second position|
|WHERE NAME LIKE 's_%'||Finds any values that start with "s" and are at least 2 characters in length|
|WHERE NAME LIKE 's__%'||Finds any values that start with "s" and are at least 3 characters in length|
|WHERE NAME LIKE 's%o'||Finds any values that start with "s" and ends with "o"|