两个查询结果,一个结果中包含另一结果,怎么把包含的结果去掉

buddyli 2005-01-07 09:12:10
两个查询结果,一个结果中包含另一结果,怎么把包含的结果去掉
...全文
145 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
SWEET_SAINT 2005-01-07
  • 打赏
  • 举报
回复
select distinct bc.a
from (select a from b
union all
select a from c) bc

看 這樣可以嗎?
老宛 2005-01-07
  • 打赏
  • 举报
回复
--测试
create table #a(
a int,
b int
)
create table #b(
a int,
b int
)
insert into #a select 1,1
insert into #a select 2,1
insert into #a select 3,1
insert into #a select 4,1
insert into #a select 1,2
insert into #a select 2,2
insert into #a select 3,2

insert into #b select 1,1
insert into #b select 2,1
insert into #b select 3,1
insert into #b select 1,1
insert into #b select 5,1
--交集
select a,b from (
select distinct a,b from #a
union all
select distinct a,b from #b
) x group by a,b having count(*)>1
--A表减交集 这个应该就是你所要的结果
select a,b from (
select a,b from (
select distinct a,b from #a
union all
select distinct a,b from #b
) x group by a,b having count(*)=1
union all
select distinct a,b from #a
) y group by a,b having count(*)>1
--B表减交集
select a,b from (
select a,b from (
select distinct a,b from #a
union all
select distinct a,b from #b
) x group by a,b having count(*)=1
union all
select distinct a,b from #b
) y group by a,b having count(*)>1
drop table #a
drop table #b
buddyli 2005-01-07
  • 打赏
  • 举报
回复
select a from b

select a from c

b,c两个查询结果,b包含c,把b中包含c的结果去掉
僵哥 2005-01-07
  • 打赏
  • 举报
回复
要具体看资料是什么样的,你的查询是怎么样写来确认。
yorbo 2005-01-07
  • 打赏
  • 举报
回复
请你具体点,包含是什么意思?

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧