如何从2个表中检索不常见的值?

Sun*_*thy 0 mysql sql-server

我有以下2个表t1,t2的值,

t1        t2
1         4
2         2
3         3
Run Code Online (Sandbox Code Playgroud)

现在我要输出

1 
4
Run Code Online (Sandbox Code Playgroud)

如何在选择查询中获得此输出?

Guf*_*ffa 5

这将为您提供t1不存在的t2每个项目,并且其中的每个项目t2都不存在于t1:

select t1.id from t1
left join t2 on t2.id = t1.id
where t2.id is null

union all

select t2.id from t2
left join t1 on t1.id = t2.id
where t1.id is null
Run Code Online (Sandbox Code Playgroud)

(我假设每个表中的字段名称id只是为了能够针对表编写查询而命名.)

另一种方式是:

select coalesce(t1.id, t2.id)
from t1
full outer join t2 on t2.id = t1.id
where t1.id is null or t2.id is null
Run Code Online (Sandbox Code Playgroud)