查询问题

jqka007 2003-10-20 04:43:28
如何在两个结构完全相同的表a和b中,查询出,在a中存在,在b中不存在的记录
...全文
44 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
gmlxf 2003-10-20
  • 打赏
  • 举报
回复
myflok(阿棋) 的啊
pengdali 2003-10-20
  • 打赏
  • 举报
回复
并运算
select c1,c2 from t1
union all
select c1,c2 from t2

差:
c1-c2:

select * from t1 where not exists(select 1 from t2 where t1.c1=t2.c1 and t1.c2=t2.c2)

c2-c1:

select * from t2 where not exists(select 1 from t1 where t1.c1=t2.c1 and t1.c2=t2.c2)

交:
select * from t1 where exists(select 1 from t2 where t1.c1=t2.c1 and t1.c2=t2.c2)
lijinqiang 2003-10-20
  • 打赏
  • 举报
回复
SELECT * FROM A WHERE B NOT IN (SELECT * FROM B )
qdubit 2003-10-20
  • 打赏
  • 举报
回复
1:如果有关键字
Select * from a where keyField not in (select keyField from b)

2:如果没有关键字,或关键字为几个字段
Select a.* from a left join b on a.F1 = b.F1 and a.F2 = b.F2 and ...
where b.F1 is null or b.F2 is null or ...
ysycrazy 2003-10-20
  • 打赏
  • 举报
回复
如果有个主键ID;
select * from a where id not in (select id from b)
hongyan80 2003-10-20
  • 打赏
  • 举报
回复
如果有主键,主键相同,记录不一定相同,所以用主键来判断是不正确的。
hongyan80 2003-10-20
  • 打赏
  • 举报
回复
如果表有主键,主键相同,则记录也相同
select * from a where 主键 not in (select 主键 from b)

一般情况
select * from a where 字段1+字段2+...... not in(select 字段1+字段2+...... from b)
Wally_wu 2003-10-20
  • 打赏
  • 举报
回复
select * from a where not exists(select * from b)
dafu71 2003-10-20
  • 打赏
  • 举报
回复
a-b

select * from a where not exists(select 1 from b where a.列1=b.列1 and a.列2=b.列2 and a.列3=b.列3 and ... and a.列n=b.列n)
伍子V5 2003-10-20
  • 打赏
  • 举报
回复
select * from a where id not in (select id from b)
sdhdy 2003-10-20
  • 打赏
  • 举报
回复
如果只有一个主键的话

select * from a where 主键 not in (select 主键 from b)
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
2:如果没有关键字,或关键字为几个字段
Select a.* from a left join b on a.F1 = b.F1 and a.F2 = b.F2 and ...
where b.F1 is null or b.F2 is null or ...
yujohny 2003-10-20
  • 打赏
  • 举报
回复
是关键字段
yujohny 2003-10-20
  • 打赏
  • 举报
回复
我上面的语句id就是你们a、b表中的关系字段
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
1:如果有关键字
Select * from a where keyField not in (select keyField from b)
yujohny 2003-10-20
  • 打赏
  • 举报
回复
select a.* from a left join b on b.id=a.id where b.id is null
wangxia241 2003-10-20
  • 打赏
  • 举报
回复
up

34,590

社区成员

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

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