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視圖?的詳細內容。更多資訊請關注PHP中文網其他相關文章!