MYSQL And & Or Operator
Category : TECH Author : Ankur Rastogi Date : Sat Apr 16 2016 Views : 29

Overview
While using where condition in mysql query we can use AND and OR operands.
Syntax
Syntax for using AND and OR operator are
SELECT * FROM table_name where [conditions];
Example
Let us dig deeper with the help of an example
Consider table hostel_list containing list of hostels.
+----+-------------------+-----------+----------+
| id | name | occupancy | capacity |
+----+-------------------+-----------+----------+
| 1 | ASHOK BHAVAN | 2 | 1090 |
| 2 | KAILASH BHAVAN | 2 | 990 |
| 3 | RUDRAKSH BHAVAN | 2 | 990 |
| 4 | KAVERI BHAVAN | 1 | 690 |
| 5 | BHAGIRATHI BHAVAN | 4 | 2140 |
| 6 | NARMADA BHAVAN | 6 | 3240 |
| 7 | GANGA BHAVAN | 1 | 780 |
| 8 | YAMUNA BHAVAN | 8 | 4580 |
+----+-------------------+-----------+----------+
If you want to fetch data where occupancy is 2 and capacity is 1090 or capacity is 690, Lets write a mysql for this requirement.
SELECT * FROM hostel_list WHERE occupancy = 2 AND (capacity = 1090 OR capacity = 690);
+----+--------------+-----------+----------+
| id | name | occupancy | capacity |
+----+--------------+-----------+----------+
| 1 | ASHOK BHAVAN | 2 | 1090 |
+----+--------------+-----------+----------+

Overview
While using where condition in mysql query we can use AND and OR operands.
Syntax
Syntax for using AND and OR operator are
SELECT * FROM table_name where [conditions];
Example
Let us dig deeper with the help of an example
Consider table hostel_list containing list of hostels.
+----+-------------------+-----------+----------+
| id | name | occupancy | capacity |
+----+-------------------+-----------+----------+
| 1 | ASHOK BHAVAN | 2 | 1090 |
| 2 | KAILASH BHAVAN | 2 | 990 |
| 3 | RUDRAKSH BHAVAN | 2 | 990 |
| 4 | KAVERI BHAVAN | 1 | 690 |
| 5 | BHAGIRATHI BHAVAN | 4 | 2140 |
| 6 | NARMADA BHAVAN | 6 | 3240 |
| 7 | GANGA BHAVAN | 1 | 780 |
| 8 | YAMUNA BHAVAN | 8 | 4580 |
+----+-------------------+-----------+----------+
If you want to fetch data where occupancy is 2 and capacity is 1090 or capacity is 690, Lets write a mysql for this requirement.
SELECT * FROM hostel_list WHERE occupancy = 2 AND (capacity = 1090 OR capacity = 690);
+----+--------------+-----------+----------+
| id | name | occupancy | capacity |
+----+--------------+-----------+----------+
| 1 | ASHOK BHAVAN | 2 | 1090 |
+----+--------------+-----------+----------+
Disclaimer: The above content reflect author’s personal views and do not reflect the views of OYEWIKI. Neither OYEWIKI nor any person/organization acting on its behalf is liable to accept any legal liability/responsibility for any error/mislead in this information or any information available on the website. This website in no way accepts the responsibility for any loss, injury, damage, discomfort or inconvenience caused as a result of reliance on any information provided on this website.
If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.