如果我們想創(chuàng)建一個(gè)視圖,根據(jù)某些特定條件從表中獲取值,那么我們必須在創(chuàng)建視圖時(shí)使用 WHERE 子句。取決于 WHERE 子句的值將存儲(chǔ)在視圖中。使用 WHERE 子句創(chuàng)建 MySQL 視圖的語(yǔ)法如下 –
語(yǔ)法
Create View view_name AS Select_statements FROM table WHERE condition(s);
登錄后復(fù)制
示例
為了說(shuō)明上述概念,我們使用表“Student_info”中的以下數(shù)據(jù) –
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)
登錄后復(fù)制
現(xiàn)在,借助以下查詢(xún),我們將創(chuàng)建視圖名稱(chēng)“Info”,其條件是存儲(chǔ)僅以計(jì)算機(jī)作為主題的行。因此,我們?cè)趧?chuàng)建視圖時(shí)需要使用 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)
登錄后復(fù)制
以上就是如何創(chuàng)建一個(gè) MySQL 視圖,根據(jù)某些條件從表中獲取值?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!