gpt4 book ai didi

sql - Union - 仅删除由 Union 创建的重复项

转载 作者:行者123 更新时间:2023-12-02 08:31:11 30 4
gpt4 key购买 nike

我想在两个表上使用联合运算符。我希望结果集消除由联合创建的重复值,但不消除表中预先存在的重复值。考虑这段代码...

select
b
from
(values (1), (2), (2), (3)) as a(b)
union
select b from (values (2), (4)) as c(b)

我希望生成一个包含两个 2 而不是三个 2 的结果集。当然,目前我得到一个 2,如果我改用 UNION ALL,我会得到三个 2。有什么可以做的吗?

最佳答案

你这样说:

select * from foo
UNION ALL
select * from bar
where not exists ( select * from foo where foo.x = bar.x )

关于sql - Union - 仅删除由 Union 创建的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26614311/

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