有个简单的查询问题,求教下大家,谢谢。

zongliang2015 2015-10-17 08:53:54
f = {3, 1, 0}

表A(8个)
f1 f2 f3
1 1 1
1 1 3
1 3 1
3 1 1
1 3 3
3 1 3
3 3 1
3 3 3

表B(27个)
f1 f2 f3
0 0 0
0 0 1
...
3 3 3

求B-A的集合,就是在表B中,不在表A中?共(27-8个)
...全文
158 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
frankl123 2015-10-19
  • 打赏
  • 举报
回复
select * from b
except 
select * from a
道玄希言 2015-10-18
  • 打赏
  • 举报
回复

;with tb as
(
  select  
    t1.f1, t1.f2, t1.f3, t2.f1 as f4 
    from 表B as t1
  left join 表A as t2 
    on t1.f1 = t2.f1 
      and t1.f2 = t2.f2 
      and t1.f3 = t2.f3 
)
select f1, f2, f3 from tb where f4 is null
symboled 2015-10-18
  • 打赏
  • 举报
回复
引用 2 楼 qq_17482963 的回复:
select * from 表B a where not exists (select * from 表A where f1=a.f1 and f2=b.f2 and f3=a.f3)
试试这个
这种在数据两很打的时候会影响速度的
qq_17482963 2015-10-18
  • 打赏
  • 举报
回复
select * from 表B a where not exists (select * from 表A where f1=a.f1 and f2=b.f2 and f3=a.f3)
试试这个
zongliang2015 2015-10-17
  • 打赏
  • 举报
回复
没人回复,自己顶下。

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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