gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:25:05 25 4
gpt4 key购买 nike

我有以下 2 个表 t1、t2 及其值,

t1        t2
1 4
2 2
3 3

现在我要输出

1 
4

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

最佳答案

这将为您提供 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

(我假设每个表中的字段名称都命名为 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

关于mysql - 如何从 2 个表中检索不常见的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6908388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com