Select*fromStudent_info;+------+---------+--------- ---+----------------+|id |Name |Address |Sub"/> Select*fromStudent_info;+------+---------+--------- ---+----------------+|id |Name |Address |Sub">

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?

王林
王林forward
2023-08-29 23:13:08863browse

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)

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)

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)

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete