SQL Condition - LIKE
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
LIKE is used in SQL to Specify a pattern.
The following patterns can be used with LIKE clause. The patterns can be used in combination.
Pattern
Description
%
Match string with zero or more characters
_
Match any single character
Imagine we have table called CUSTOMER, with the following data:
FIRSTNAME
LASTNAME
AGE
COUNTRY
Rambo
Robert
25
Belgium
Mugambo
Satraj
37
Norway
Nagashekar
Rao
47
India
Tom
Harry
27
Brazil
Dana
Laura
21
Australia
Rambo
Fox
57
New Zealand
Giselle
Chivvi
87
Japan
SELECT * from Customer WHERE COUNTRY LIKE 'N%'
Result:
FIRSTNAME
LASTNAME
AGE
COUNTRY
Mugambo
Satraj
37
Norway
Rambo
Fox
57
New Zealand
Scenario 2: Get me all records where Customer's Country does NOT start with N. We can use a NOT LIKE Clause with % to specify the above criteria.
SELECT * from Customer WHERE COUNTRY NOT LIKE 'N%'
Result:
FIRSTNAME
LASTNAME
AGE
COUNTRY
Rambo
Robert
25
Belgium
Nagashekar
Rao
47
India
Tom
Harry
27
Brazil
Dana
Laura
21
Australia
Giselle
Chivvi
87
Japan
Scenario 3: Get me all records where Customer's Name is of three characters and starts with T. We can use a LIKE Clause with _ to specify the above criteria.
SELECT * from Customer WHERE FIRSTNAME LIKE 'T__'
Result:
FIRSTNAME
LASTNAME
AGE
COUNTRY
Tom
Harry
27
Brazil