简体   繁体   English

如何使用mysql将多个表合并为1

[英]how to combine multiple tables into 1 with mysql

I have a Mysql DB with 4 tables, with same structure and I need to merge all of this tables in a 1 table.我有一个带有 4 个表的 Mysql DB,具有相同的结构,我需要将所有这些表合并到 1 个表中。

There are 7 fields that are the same in all tables.所有表中有 7 个字段都相同。

Is there any way to merge the tables ?有什么办法可以合并表吗?

Thank you very much非常感谢

If you want just to query all the data from the tables and present them into one set of results you can try the following:如果您只想查询表中的所有数据并将它们呈现为一组结果,您可以尝试以下操作:

SELECT * FROM table1
UNION
SELECT * FROM table2
UNION
SELECT * FROM table3
UNION
SELECT * FROM table4

If you actually want to transfer all the data from table2, table3, table4 to table1 you can try something like the following:如果您真的想将 table2、table3、table4 中的所有数据传输到 table1,您可以尝试以下操作:

INSERT INTO table1 (Col1, Col2...col7)
SELECT * FROM table1
UNION
SELECT * FROM table2
UNION
SELECT * FROM table3
UNION
SELECT * FROM table4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM