如果我們想要建立一個視圖,根據某些特定條件從表格中取得值,那麼我們必須在建立視圖時使用 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中文網其他相關文章!