如果我们想创建一个视图,根据某些特定条件从表中获取值,那么我们必须在创建视图时使用 WHERE 子句。取决于 WHERE 子句的值将存储在视图中。使用 WHERE 子句创建 MySQL 视图的语法如下 -
Create View view_name AS Select_statements FROM table WHERE condition(s);
为了说明上述概念,我们使用表“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 | +------+---------+------------+------------+ 4 rows in set (0.08 sec)
现在,借助以下查询,我们将创建视图名称“Info”,其条件是存储仅以计算机作为主题的行。因此,我们在创建视图时需要使用 WHERE 子句,如下所示 -
mysql> Create OR Replace VIEW Info AS Select Id, Name, Address, Subject from student_info WHERE Subject = 'Computers'; Query OK, 0 rows affected (0.46 sec) mysql> Select * from info; +------+-------+---------+-----------+ | Id | Name | Address | Subject | +------+-------+---------+-----------+ | 125 | Raman | Shimla | Computers | | 130 | Ram | Jhansi | Computers | +------+-------+---------+-----------+ 2 rows in set (0.00 sec)
以上是如何创建一个 MySQL 视图,根据某些条件从表中获取值?的详细内容。更多信息请关注PHP中文网其他相关文章!