如何在ID上合并两个表

时间:2015-12-21 11:48:23

标签: mysql sql

我有两个表,它们具有完全相同的列。我想将表b合并到表a中,如果数据集具有相同的ID,我想使用表b的数据集。

我尝试过类似的事情:

SELECT *
FROM
((SELECT 
    *
FROM
    tableA) UNION  (SELECT 
    *
FROM
    tableB)) AS temp
GROUP BY temp.ID
ORDER BY temp.ID

但这给了我两个表的混合。

1 个答案:

答案 0 :(得分:2)

您可以使用union all以及其他一些逻辑执行此操作:

select b.*
from b
union all
select a.*
from a
where not exists (select 1 from b where b.id = a.id);
相关问题