
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.mryunwei.com)其它相关文章!


