A表B表结构一样,怎么查出A表中有B表中没有的数据?SQL语句怎么写??

dexterloo 2009-03-09 01:09:42
A表B表结构一样,怎么查出A表中有B表中没有的数据?SQL语句怎么写??
...全文
511 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdsnhan 2009-03-10
  • 打赏
  • 举报
回复
这问题问的。
你数据库是啥也不说
li_d_s 2009-03-09
  • 打赏
  • 举报
回复
CSDN怎么老是连自己回复的帖子都看不见?怪哉
dawugui 2009-03-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dengmingcai 的回复:]
在SQL中只能这样,如果在ORACLE中有个差集的函数,但在SQL没有。
[/Quote]
如何从表A(总表)中找出表B(分表)中不存在的记录组合

假设表A和表B都只有两个字段id,name
如何用一句SQL返回表A中存在的id,name结果集而在表B中不存在的id,name结果集

select A.* from A left join B on A.id=B.id and A.name=B.name where B.id is null

select * from A where not exists(select top 1 * from B where A.ID=B.ID)

这两个都可以.


--前提:表中不能有text、ntext、image、cursor 数据类型的字段。

用CheckSum()最简单:

select * from A where checksum(*) not in (select checksum(*) from B)

----------------------------------------------------------------------


查询表A,表B中重复的记录

select * from A where checksum(*) in (select checksum(*) from B)

查询表A,表B中重复的记录,并把结果输入到表C中。

select * into c from A where checksum(*) in (select checksum(*) from B)
PB菜鸟 2009-03-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 li_d_s 的回复:]
select a.* from A a where not exists (select 1 from B b where b.pk = a.pk)
[/Quote]
正解
dengmingcai 2009-03-09
  • 打赏
  • 举报
回复
在SQL中只能这样,如果在ORACLE中有个差集的函数,但在SQL没有。
li_d_s 2009-03-09
  • 打赏
  • 举报
回复
select a.* from A a where not exists (select 1 from B b where b.pk = a.pk)

752

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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