如何通过在基本表中进行模式匹配来选择数据,从而创建 MySQL 视图?
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)
示例
以下查询将创建一个名为“信息”的视图,使用“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)
广告