Home > Database > Mysql Tutorial > How can we create a MySQL view by selecting data based on pattern matching from a base table?

How can we create a MySQL view by selecting data based on pattern matching from a base table?

王林
Release: 2023-08-29 23:13:08
forward
859 people have browsed it

How can we create a MySQL view by selecting data based on pattern matching from a base table?

#MySQL LIKE operator is used to select data based on pattern matching. Likewise, we can use the LIKE operator with a view to select specific data based on pattern matching in the base table. To understand this concept, we use the base table "student_info" with the following data -

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)
Copy after login

Example

The following query will create a view named "Info" using the "LIKE" operator Select some specific values ​​based on pattern matching-

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)
Copy after login

We can also use NOT with LIKE operator as follows-

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)
Copy after login

The above is the detailed content of How can we create a MySQL view by selecting data based on pattern matching from a base table?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template