我們知道 MySQL BETWEEN 運算子可用來從某個值範圍中選擇值。我們可以使用 BETWEEN 運算子和視圖來從基底表中選擇某些範圍的值。為了理解這個概念,我們使用具有以下資料的基底表「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」的視圖,透過使用「BETWEEN」運算符選擇特定範圍內的某些值-
mysql> Create or Replace view Info AS SELECT * from student_info WHERE Name BETWEEN 'C' AND 'P'; Query OK, 0 rows affected (0.14 sec) mysql> Select * from info; +------+--------+------------+------------+ | id | Name | Address | Subject | +------+--------+------------+------------+ | 105 | Gaurav | Chandigarh | Literature | | 133 | Mohan | Delhi | Computers | +------+--------+------------+------------+ 2 rows in set (0.00 sec)
同樣,我們可以將NOT 與BETWEEN 運算子結合使用來選擇與查詢中寫入的值不同的範圍-
mysql> Create or Replace view Info AS SELECT * from student_info WHERE Name NOT BETWEEN 'C' AND 'P'; Query OK, 0 rows affected (0.06 sec) mysql> Select * from Info; +------+---------+------------+-----------+ | id | Name | Address | Subject | +------+---------+------------+-----------+ | 101 | YashPal | Amritsar | History | | 125 | Raman | Shimla | Computers | | 130 | Ram | Jhansi | Computers | | 132 | Shyam | Chandigarh | Economics | +------+---------+------------+-----------+ 4 rows in set (0.00 sec)
以上是我們如何透過從基底表中選擇某些範圍的值來建立 MySQL 視圖?的詳細內容。更多資訊請關注PHP中文網其他相關文章!