SQL LIKE query Command
Using Like Query with wildcard in different combinations, we can match our keyword with the pattern of the data present in columns.
The best way to use LIKE command is to apply it against a text or varchar field along with wildcard % or _
Video Tutorial on LIKE Query with AND , OR NOT combinations.
.
Here is our table with all the records.
1 | John Deo | Four | 75 |
2 | Max Ruin | Three | 85 |
3 | Arnold | Three | 55 |
4 | Krish Star | Four | 60 |
5 | John Mike | Four | 60 |
6 | Alex John | Four | 55 |
We will apply the LIKE command here like this to our name field. SELECT * FROM student WHERE name LIKE '%John%'
1 | John Deo | Four | 75 |
5 | John Mike | Four | 60 |
6 | Alex John | Four | 55 |
Read how Regular expression is used to Pattern matching
SELECT * FROM student WHERE name LIKE 'John%'1 | John Deo | Four | 75 |
5 | John Mike | Four | 60 |
SELECT * FROM student WHERE name LIKE '%John'
6 | Alex John | Four | 55 |
The above result have desired word John at the end only.
Matching string in name column starting with A and ending with n.
'%John' | Matches string ending with John |
'John%' | Matches string starting with John |
'%John%' | Matches string anywhere with John |
'j%n' | Matches string starting with j and ending with n |
Use of underscore [ _] as wildcard in string matching
We can use underscore as wildcard for one character space and use them along with LIKE statement and apply to table columns. For example we want to collect all the account numbers ending with 044 in a five digit account number field. Here is the query for this. SELECT * FROM account_master WHERE acc_no LIKE '__044' We have used two underscores in our query to tell that first two digits can be any thing and it should end with 044.Underscores as wildcard can be used at any location but one can replace one character only. We can use more than one underscore also inside our query.
Using NOT with LIKE
SELECT * FROM student WHERE name NOT LIKE '%John%' This way we can use LIKE command with many other commands to get desired output.Case Sensitive query
As we have seen all the above cases are case insensitive. To match lower only or upper only cases we have to use binary command to make binary matching. Here is an example. select * from student where name LIKE binary '%A' Try the same query by not using binary inside it.Name of the students having letter 'a' and letter 'e' [ without single quotes ]
SELECT * FROM student WHERE name LIKE '%a%' AND name LIKE '%e%' Find all courses from the Section table that start with the character, C, but do not have h, as the second character. SELECT * FROM student WHERE name LIKE 'C%' AND name NOT LIKE '_h%'Searching keyword across multiple columns by using AND , OR
Keyword should present in both columns [ by using AND ] SELECT * FROM TABLE_NAME WHERE Column1 Like '%keyword%' AND Column2 LIKE '%keyword%' Keyword should present in any columns [ by using OR ] SELECT * FROM TABLE_NAME WHERE Column1 Like '%keyword%' OR Column2 LIKE '%keyword%' Same way the query can be extended to search across more than two columns.LIKE with CONCAT
In the string supplied the name is matched. [Reverse way of matching] SELECT * FROM student WHERE ' Our best student is Mr John Deo of 5th Class' LIKE CONCAT['%',name,'%']CONCAT to join strings in Query
Limitation of Like query
Like condition has to match the entire data where as by using Regular Expression REGEXP we can match anywhere within the dataDisplaying records using PHP Script
Here is the sample code.