A、B两个表的模式完全相同,如何知道小表A的所有记录都包含在大表B中,请写出其SQL语句

zcdg909 2008-03-24 01:36:25
A、B两个表的模式完全相同,如何知道小表A的所有记录都包含在大表B中,请写出其SQL语句
...全文
95 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzy_love_sly 2008-03-24
  • 打赏
  • 举报
回复
declare @a table (id int)
insert into @a select 1
insert into @a select 3

declare @b table (id int)
insert into @b select 1
insert into @b select 2
insert into @b select 3

declare @counta int,@countb int
select @counta=count(1) from @a
select @countb=count(1) from @b b where exists(select 1 from @a where id=b.id)
if @counta=@countb
print '全部包括'
else
print '不全部包括'




不全部包括
青锋-SS 2008-03-24
  • 打赏
  • 举报
回复
select * from a where not exists(select 1 from b where a.col1=b.col1 and a.col2=b.col2 and ......)

如果上述查询没有结果,就说明A的所有记录都在B中

34,594

社区成员

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

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