
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- How can we use a combination of logical operators while creating MySQL views?
- Can we use {} while creating a MySQL table?
- How can we create MySQL views?
- How can we create MySQL views with column list?
- How can we create MySQL views without any column list?
- Where MySQL views can be inconsistent and how can we ensure their consistency?
- While creating a MySQL table use the reserved keyword ‘Key’
- Python Logical Operators
- Java Logical Operators
- Perl Logical Operators
- Creating views in MongoDB
- Can we combine MySQL IN AND LIKE operators?
- Why in MySQL, we cannot use arithmetic operators like ‘=’, ‘
- What is MySQL NOT NULL constraint and how can we declare a field NOT NULL while creating a table?
- Can we use INTERVAL keyword while inserting date records in a MySQL table?
