简体   繁体   English

MySQL 将两个表合并在一起

[英]MySQL Merge Two Tables together

I'm not the best in MySQL but currently on a tidy up task, not sure if this is possible or ill have to make a script to do this using inserts.我不是 MySQL 中最好的,但目前正在进行一项整理任务,不确定这是否可能,或者我必须使用插入来制作脚本来执行此操作。

But i have two tables AccountInfo and AccountLogin they both share the same PK so id1 will match id1 in the other table.但是我有两个表 AccountInfo 和 AccountLogin 它们都共享相同的 PK,因此 id1 将匹配另一个表中的 id1。 What im trying to do is get both tables merged into one table either called Account or merge into a table of the two and rename after.我想要做的是将两个表合并到一个名为 Account 的表中,或者合并到两个表中,然后重命名。

Any help or advice will be appreciated.任何帮助或建议将不胜感激。

Create table "Members" with columns: id, other columns of AccountInfo, other columns of AccountLogin.创建表“成员”,其列:id、AccountInfo 的其他列、AccountLogin 的其他列。

Then, like Lelio Faieta said, INSERT INTO Members SELECT id, other columns of AccountInfo ai, other columns of AccountLogin al FROM AccountInfo ai INNER JOIN AccountLogin al ON (al.id = ai.id).然后,就像 Lelio Faieta 说的,INSERT INTO Members SELECT id,AccountInfo ai 的其他列,AccountLogin al 的其他列 FROM AccountInfo ai INNER JOIN AccountLogin al ON (al.id = ai.id)。

Also, if there are AccountInfo rows without matching AccountLogin rows you can use a LEFT JOIN instead of an INNER JOIN.此外,如果存在没有匹配 AccountLogin 行的 AccountInfo 行,您可以使用 LEFT JOIN 而不是 INNER JOIN。

Disclosure: I usually use Oracle, so I might make a mistake with MySQL.披露:我通常使用 Oracle,所以我可能会误用 MySQL。

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

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