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 视图?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!