SQL Condition - IN and NOT IN
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
Using IN Statement in WHERE Clause, we can select a subset of rows from the Table which satisfy the values present in the IN Clause.
Using NOT IN statement in WHERE Clause, we can select a subset of rows from the Table which satisfy the values not present in the IN Clause. Let us see examples to make this clear.
Imagine we have table called CUSTOMERS, 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 IN ('Belgium', 'India', 'Brazil')
Result:
FIRSTNAME
LASTNAME
AGE
COUNTRY
Rambo
Robert
25
Belgium
Nagashekar
Rao
47
India
Tom
Harry
27
Brazil
Scenario 2: Get me all records where Customer's Country is NOT in Belgium, India and Brazil. We can use a NOT IN Clause to specify the above criteria.
SELECT * from Customer WHERE COUNTRY NOT IN ('Belgium', 'India', 'Brazil')
Result:
FIRSTNAME
LASTNAME
AGE
COUNTRY
Mugambo
Satraj
37
Norway
Dana
Laura
21
Australia
Rambo
Fox
57
New Zealand
Giselle
Chivvi
87
Japan