The MySQL like clause is a string comparison operator which gives an approximate match to a term. The like clause differs from the equal sign ( = ) which is used when we want to match a term exactly.
By using the % character (which is a wildcard) with the MySQL like clause, we are able to match terms with varying degrees of precision.
Have a look at our MySQL like clause tutorial below and get yourself up to speed.
The MySQL like clause can be used with a select, update, or delete query and comes after the where clause like this:
SELECT * FROM employees WHERE last_name LIKE '%something%';
The following are a few guidelines for the like clause:
The % symbol is used to match any number of characters.
Let us assume we have the following table (called "employees") in our database:
first_name | last_name |
---|---|
Paul | Pitterson |
Francine | Beecham |
Raul | DiNozzo |
Anthony | DiNozzo |
Vincent | Taylor |
Kevin | Taub |
Garth | Tyson |
Suppose we wanted to find all the persons with a last name starting with "Ta", we would use the following query:
SELECT * FROM employees WHERE last_name LIKE 'Ta%';
The result would be:
first_name | last_name |
---|---|
Vincent | Taylor |
Kevin | Taub |
As you can see, only those records with last names starting with "Ta" were returned.
Of course we could place the wildcard wherever we want depending on what we wanted to match:
SELECT * FROM employees WHERE last_name LIKE '%n';
The result would be:
first_name | last_name |
---|---|
Paul | Pitterson |
Garth | Tyson |
This time we only have last names ending in "n".
We hope this tutorial on the MySQL like clause was useful.