首页 > 解决方案 > 如何获得我的结果为 1 2 3 6 7 8 的列我想从两个表中排除公共 ID 并将结果显示在一个列中

问题描述

假设我有两个表 T1 和 T2 以 ID 作为列。例如

T1:ID

1              
2              
3              
4              
5   

T2:ID

4
5
6
7
8

如何获得我的结果为 1 2 3 6 7 8 的列我想从两个表中排除公共 ID 并将结果显示在一个列中。

标签: sqljoin

解决方案


嗯。. . 一种方法是union all

select t1.id
from table1 t1
where not exists (select 1 from table2 t2 where t2.id = t1.id)
union all
select t1.id
from table2 t2
where not exists (select 1 from table1 t1 where t2.id = t1.id);

推荐阅读