We know that MySQL BETWEEN operator can be used to select a value from a range of values. We can use the BETWEEN operator with views to select a certain range of values from the base table. To understand this concept, we use the base table "student_info" with 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)
The following query will create a view named "Info" by using the "BETWEEN" operation operator to select certain values within a specific range -
mysql> Create or Replace view Info AS SELECT * from student_info WHERE Name BETWEEN 'C' AND 'P'; Query OK, 0 rows affected (0.14 sec) mysql> Select * from info; +------+--------+------------+------------+ | id | Name | Address | Subject | +------+--------+------------+------------+ | 105 | Gaurav | Chandigarh | Literature | | 133 | Mohan | Delhi | Computers | +------+--------+------------+------------+ 2 rows in set (0.00 sec)
Similarly, we can use NOT with the BETWEEN operator to select a range that is different from the values written in the query-
mysql> Create or Replace view Info AS SELECT * from student_info WHERE Name NOT BETWEEN 'C' AND 'P'; 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 | | 132 | Shyam | Chandigarh | Economics | +------+---------+------------+-----------+ 4 rows in set (0.00 sec)
The above is the detailed content of How can we create a MySQL view by selecting some range of values from a base table?. For more information, please follow other related articles on the PHP Chinese website!