我有两个表,我想合并它们。我只需要保留两个表之一的重复项。我试图找到它,但找不到任何地方。希望有人能帮忙。
例如: 表_1:
表_2:
期望的结果:
所以总是使用 table_2 中的重复项。在这个例子中ID 3是重复的,所以使用table_2的副本,数量为9。
如何使用 T-SQL 实现这一点?我使用了下面的代码:
Select * from Table_1 where Table_1.id != Table_2.id
Union All
Select * from Table_2
但随后我收到错误: '无法绑定(bind)多部分标识符“Table_2.ID”。'
最佳答案
使用不存在
:
Select t1.*
from Table_1 t1
where not exists (select 1 from table_2 t2 where t2.id = t1.id)
Union All
Select t2.*
from Table_2 t2;
关于sql - Union All 但只保留 T-SQL 中一个表中的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67738050/