I'd a trouble with inserting data from 3 table: A (id, name), B (id, name), C (id, name). They have the same field like that. How can I insert data from 3 tables above into table D (id, name)?
Asked
Active
Viewed 24 times
1 Answers
4
You could use UNION
or UNION ALL
INSERT INTO table_d(id, name)
SELECT id, name
FROM table_a
UNION ALL
SELECT id, name
FROM table_b
UNION ALL
SELECT id, name
FROM table_c;
If you want to remove duplicate rows in 3 tables, change UNION ALL
to UNION
. Refer information about union vs union all

Pham X. Bach
- 5,284
- 4
- 28
- 42
-
2You're missing one UNION (ALL) for table_c (and didn't say when to use UNION, and when UNION ALL). – Littlefoot Mar 25 '19 at 06:05