我们知道 MySQL BETWEEN 运算符可用于从某个值范围中选择值。我们可以使用 BETWEEN 运算符和视图来从基表中选择某些范围的值。为了理解这个概念,我们使用具有以下数据的基表“student_info” -
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)
以下查询将创建一个名为“Info”的视图,通过使用“BETWEEN”运算符选择特定范围内的某些值 -
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)
同样,我们可以将 NOT 与 BETWEEN 运算符结合使用来选择与查询中写入的值不同的范围 -
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)
以上是我们如何通过从基表中选择某些范围的值来创建 MySQL 视图?的详细内容。更多信息请关注PHP中文网其他相关文章!