MySQL LIKE 運算符用于根據模式匹配選擇數據。同樣,我們可以將 LIKE 運算符與視圖結合使用,根據基表中的模式匹配來選擇特定數據。為了理解這個概念,我們使用具有以下數據的基表“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”的視圖,使用“LIKE”運算符根據模式匹配選擇一些特定值 –
mysql> Create or Replace view Info AS SELECT * from student_info WHERE Name LIKE '%Ra%'; Query OK, 0 rows affected (0.11 sec) mysql> Select * from Info; +------+--------+------------+------------+ | id | Name | Address | Subject | +------+--------+------------+------------+ | 105 | Gaurav | Chandigarh | Literature | | 125 | Raman | Shimla | Computers | | 130 | Ram | Jhansi | Computers | +------+--------+------------+------------+ 3 rows in set (0.00 sec)
登錄后復制
我們也可以將 NOT 與 LIKE 運算符一起使用,如下 –
mysql> Create or Replace view Info AS SELECT * from student_info WHERE Name NOT LIKE'%Ra%'; Query OK, 0 rows affected (0.14 sec) mysql> Select * from info; +------+---------+------------+-----------+ | id | Name | Address | Subject | +------+---------+------------+-----------+ | 101 | YashPal | Amritsar | History | | 132 | Shyam | Chandigarh | Economics | | 133 | Mohan | Delhi | Computers | +------+---------+------------+-----------+ 3 rows in set (0.00 sec)
登錄后復制
以上就是我們如何通過從基表中基于模式匹配選擇數據來創建MySQL視圖?的詳細內容,更多請關注www.92cms.cn其它相關文章!