Consider tables table1
, table2
, table3
table1
:
+------+------+
| col1 | col2 |
+------+------+
| 1 | a |
| 2 | b |
| 3 | c |
+------+------+
table2
:
+------+------+
| col1 | col2 |
+------+------+
| 2 | b |
| 3 | c |
| 4 | d |
+------+------+
table3
:
+------+------+
| col1 | col2 |
+------+------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
+------+------+
How do I create table3
out of table1
and table2
in MySQL 5.7.12? What is the name of this operation?
Here is code to create table1
and table2
:
DROP TABLE table1;
CREATE TABLE table1 (
col1 BIGINT,
col2 TEXT
);
INSERT INTO table1 VALUES (1, 'a');
INSERT INTO table1 VALUES (2, 'b');
INSERT INTO table1 VALUES (3, 'c');
DROP TABLE table2;
CREATE TABLE table2 (
col1 BIGINT,
col2 TEXT
);
INSERT INTO table2 VALUES (2, 'b');
INSERT INTO table2 VALUES (3, 'c');
INSERT INTO table2 VALUES (4, 'd');
Using the UNION DISTINCT
operation:
CREATE TABLE table3 SELECT * FROM table1 UNION DISTINCT SELECT * FROM table2;