Home > Database > Mysql Tutorial > How to merge two tables using MySQL union to create a new table?

How to merge two tables using MySQL union to create a new table?

WBOY
Release: 2023-09-14 08:05:10
forward
1376 people have browsed it

How to merge two tables using MySQL union to create a new table?

The following is the syntax to merge two tables using MySQL union

create table yourTableName
(
   select *from yourTableName1
)
UNION
(
   select *from yourTableName2
);
Copy after login

To understand the above syntax, let us create a table. The query to create the first table is as follows

mysql> create table Old_TableDemo
   -> (
   -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> UserName varchar(20)
   -> );
Query OK, 0 rows affected (0.63 sec)
Copy after login

The query to create the second table is as follows

mysql> create table Old_TableDemo2
   -> (
   -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> UserName varchar(20)
   -> );
Query OK, 0 rows affected (0.60 sec)
Copy after login

Use the insert command to insert some records in the first table. The query is as follows -

mysql> insert into Old_TableDemo(UserName) values('John');
Query OK, 1 row affected (0.22 sec)
mysql> insert into Old_TableDemo(UserName) values('Carol');
Query OK, 1 row affected (0.15 sec)
Copy after login

Use the select statement to display all records in the first table. The query is as follows -

mysql> select *from Old_TableDemo;
Copy after login

The following is the output

+--------+----------+
| UserId | UserName |
+--------+----------+
|      1 | John     |
|      2 | Carol    |
+--------+----------+
2 rows in set (0.00 sec)
Copy after login

Now you can insert some records in the second table using insert command. The query is as follows -

mysql> insert into Old_TableDemo2(UserName) values('Larry');
Query OK, 1 row affected (0.22 sec)
mysql> insert into Old_TableDemo2(UserName) values('Sam');
Query OK, 1 row affected (0.10 sec)
Copy after login

Use the select statement to display all records in the second table. The query is as follows -

mysql> select *from Old_TableDemo2;
Copy after login

The following is the output

+--------+----------+
| UserId | UserName |
+--------+----------+
|      1 | Larry    |
|      2 | Sam      |
+--------+----------+
2 rows in set (0.00 sec)
Copy after login

Here is the query to create a new table by union merging two tables

mysql> create table UserTableDemo
   -> (
   -> select *from Old_TableDemo
   -> )
   -> UNION
   -> (
   -> select *from Old_TableDemo2
   -> );
Query OK, 4 rows affected (1.18 sec)
Records: 4 Duplicates: 0 Warnings: 0
Copy after login

Let us check the table records of the new table . The query is as follows -

mysql> select *from UserTableDemo;
Copy after login

The following is the output

+--------+----------+
| UserId | UserName |
+--------+----------+
|      1 | John     |
|      2 | Carol    |
|      1 | Larry    |
|      2 | Sam      |
+--------+----------+
4 rows in set (0.00 sec)
Copy after login

The above is the detailed content of How to merge two tables using MySQL union to create a new 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