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

Home  >  Article  >  Database  >  How to create a MySQL view using data from multiple tables?

How to create a MySQL view using data from multiple tables?

王林
王林forward
2023-08-29 16:25:021256browse

如何使用多个表中的数据创建 MySQL 视图?

MySQL UNION operator can combine two or more result sets, so we can use UNION operator to create a view containing data from multiple tables. To understand this concept, we are using base tables "Student_info" and "Student_detail" 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)

mysql> Select * from Student_detail;
+-----------+-------------+------------+
| Studentid | StudentName | address    |
+-----------+-------------+------------+
|       100 | Gaurav      | Delhi      |
|       101 | Raman       | Shimla     |
|       103 | Rahul       | Jaipur     |
|       104 | Ram         | Chandigarh |
|       105 | Mohan       | Chandigarh |
+-----------+-------------+------------+
5 rows in set (0.00 sec)

Example

The following query will create a view using the data from the above two tables -

mysql> Create or Replace View Info AS Select StudentName from Student_detail UNION Select Name From Student_info;
Query OK, 0 rows affected (0.10 sec)

mysql> select * from info;
+-------------+
| StudentName |
+-------------+
| Gaurav      |
| Raman       |
| Rahul       |
| Ram         |  
| Mohan       |
| YashPal     |
| Shyam       |
+-------------+
7 rows in set (0.00 sec)

The result set above contains a combination of values ​​in both columns. If a value is duplicated then it eliminates the duplicated values.

We can also store all values ​​or repeat a value by using UNION ALL as shown in the following query -

mysql> Create or Replace View Info AS Select student name from Student_detail UNION ALL Select Name From Student_info;
Query OK, 0 rows affected (0.16 sec)

mysql> select * from info;
+-------------+
| StudentName |
+-------------+
| Gaurav      |
| Raman       |
| Rahul       |
| Ram         |
| Mohan       |
| YashPal     |
| Gaurav      |
| Raman       |
| Ram         |
| Shyam       |
| Mohan       |
+-------------+
11 rows in set (0.00 sec)

The above is the detailed content of How to create a MySQL view using data from multiple tables?. 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