SQL LIKE Query Command
LIKE sql command is used to retrieve or match records in different combinations to get desired result with wildcard. We will try LIKE sql query command and how to use it in our MySQL tables. But the best way to use LIKE query command is to apply it in a text or varchar field along with wildcard %.
SELECT * FROM student WHERE name LIKE '%John%'
We will get all the names where John word is present in the record. It can be any where. Please note the use of symbol % in the query. This will match any number of character even zero character before or after the word John. So we are getting the names those starts with John also. We may require the names which only starts with John not inside the name. In other words we want records starting with John only. In this case we will remove the use of % at the left of word John. Here is the SQL with LIKE command.
SELECT * FROM student WHERE name LIKE 'John%'
This list out all the names starting with name John. To display the records which does not have specific word John as the beginning we have change our LIKE sql command a little by changing the % position to the end of the word. Here we will allow any character even zero character to the left of the desired word not to the right
SELECT * FROM student WHERE name LIKE '%John'
This will have desired word John at the end only.