How can we use logical operators while creating MySQL views?


MySQL views can be created by using logical operators like AND, OR, and NOT. It can be illustrated with the help of following examples −

Views with AND operator

As we know that logical AND operator compares two expressions and returns true if both the expressions are true. In the following example, we are creating a view which has the conditions based on ‘AND’ operator.

Example

The base table is Student_info having the following data −

mysql> Select * from Student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Chandigarh | Literature |
| 125  | Raman   | Shimla     | Computers  |
| 130  | Ram     | Jhansi     | Computers  |
| 132  | Shyam   | Chandigarh | Economics  |
| 133  | Mohan   | Delhi      | Computers  |
+------+---------+------------+------------+
6 rows in set (0.00 sec)

mysql> Create or Replace View Info AS select ID, Name, Address , Subject FROM Student_info WHERE (Subject = 'Computers' AND ADDRESS = 'DELHI');
Query OK, 0 rows affected (0.13 sec)

mysql> Select * from Info;
+------+-------+---------+-----------+
| ID   | Name  | Address | Subject   |
+------+-------+---------+-----------+
| 133  | Mohan | Delhi   | Computers |
+------+-------+---------+-----------+
1 row in set (0.00 sec)

Views with OR operator

As we know the logical OR operator compares two expressions and returns true if at least one of the expression is true. In the following example, we are creating a view which has the conditions based on ‘OR’ operator.

Example

mysql> Create or Replace View Info AS select ID, Name, Address , Subject FROM Student_info WHERE (Subject = 'Computers' OR ADDRESS = 'Amritsar');
Query OK, 0 rows affected (0.06 sec)

mysql> Select * from Info;
+------+---------+----------+-----------+
| ID   | Name    | Address  | Subject   |
+------+---------+----------+-----------+
| 101  | YashPal | Amritsar | History   |
| 125  | Raman   | Shimla   | Computers |
| 130  | Ram     | Jhansi   | Computers |
| 133  | Mohan   | Delhi    | Computers |
+------+---------+----------+-----------+
4 rows in set (0.00 sec)

Views with NOT operator

NOT is the only operator that takes only one operand. It returns 0 if the operand is TRUE and returns 1 if the operand is FALSE. In the following example, we are creating a view which has the conditions based on ‘NOT’ operator.

Example

mysql> Create or Replace View Info AS select ID, Name, Address , Subject FROM Student_info WHERE Subject != 'Computers';
Query OK, 0 rows affected (0.06 sec)

mysql> Select * from info;
+------+---------+------------+------------+
| ID   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Chandigarh | Literature |
| 132  | Shyam   | Chandigarh | Economics  |
+------+---------+------------+------------+
3 rows in set (0.00 sec)

Updated on: 22-Jun-2020

115 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements