Home > Database > Mysql Tutorial > body text

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

王林
Release: 2023-08-29 16:25:02
forward
1285 people have browsed it

如何使用多个表中的数据创建 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)
Copy after login

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

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

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!

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