Select with AND, OR, NOT Operators tutorial in php
SELECT with AND and OR Operator Syntax :
SELECT column1, column2, column3... FROM table_name WHERE condition1 AND condition2 AND condition3...;
SELECT column1, column2, column3... FROM table_name WHERE condition1 OR condition2 OR condition3...;
If we want to select record whose age is greater than or equal to 18 and less than or equal to 21 from table name "personal" then we can use below command as shown below :
SELECT * FROM personal WHERE age >= 18 AND age <= 21;
If we want to select record whose age is less than or equal to 20 and gender is Male and city is Agra from table name "personal" then we can use below command as shown below :
SELECT * FROM personal WHERE age <= 21 AND gender = "M" AND city = "Agra";
If we want to select record whose age is less than or equal to 20 OR city is Agra from table name "personal" then we can use below command as shown below :
SELECT * FROM personal WHERE age <= 20 OR city = "Agra";
If we want to select record whose city is Agra OR Delhi from table name "personal" then we can use below command as shown below :
SELECT * FROM personal WHERE city = "Agra" OR city = "Delhi";
If we want to select record whose city is Agra OR Delhi AND gender is Male from table name "personal" then we can use below command as shown below :
SELECT * FROM personal WHERE (city = "Agra" OR city = "Delhi") AND gender = "M";
NOT Operator:
If we want to select record whose city is NOT Agra from table name "personal" then we can use below command as shown below :
SELECT * FROM personal WHERE NOT city = "Agra" OR city = "Ahmedabad";
If we want to select record whose city is NOT Agra as well as NOT Ahmedabad from table name "personal" then we can use below command as shown below :
SELECT * FROM personal WHERE NOT (city = "Agra" OR city = "Ahmedabad");
If we want to select record whose age is less than or equal to 20 using NOT Operator from table name "personal" then we can use below command as shown below :
SELECT * FROM personal WHERE NOT age > 20;
Comments
Post a Comment