简体   繁体   中英

MySQL: Merging two tables together

Just wondering, if I have two tables that have some common columns (might be in different order though) and some different columns, is there an easy way to merge the two structures together so that both of them would have the same columns?

I tried exporting the structure of both and merging the files together aiming to have a create query that creates a merged table but because the common columns are not in the same order I end up trying to add the same column twice.

Or maybe if there's a query like:

ALTER TABLE `example` ADD IGNORE...

I think you want a union all query:

select col1, col2, col3, NULL as col4, col5
from table1
union all
select col1, col2, col3, col4, NULL as col5
form table2;

You should look at 'JOIN' so if your_table has more records and you need some additional columns from your_second_table and tables are connected by some common_field column in both you can:

SELECT t.*, t2.*
FROM your_table t
LEFT JOIN your_second_table t2
ON t.common_field = t2.common_field
  AND t.another_common_field = t2.another_common_field

最终,对我有用的是导出两个表的结构,并将它们的add列查询放在excel列中,然后删除重复项,并使用剩下的唯一查询(这两个表中的唯一列)创建一个新查询。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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