我们如何通过从基表中选择一定范围的值来创建MySQL视图?
众所周知,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';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';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视图? 的全部内容, 来源链接: utcz.com/z/321998.html