各位SQL人士:如果比较A,B两个表的数据,以pd_name字段为标准,显示出A表有,B表没有的数据。怎样写?

clubsondy 2006-09-06 09:19:35
如果比较A,B两个表的数据,以pd_name字段为标准,显示出A表有,B表没有的数据。怎样写?
...全文
309 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiechifeiniao 2006-09-06
  • 打赏
  • 举报
回复
select distinct a.* from a left join b on a.pd_name =b.pd_name
where b.pd_name is null
stou 2006-09-06
  • 打赏
  • 举报
回复
樓上們正解
csdnweii 2006-09-06
  • 打赏
  • 举报
回复
not in
not exists
left join

但是not in 效率太低,最好不用
xyxfly 2006-09-06
  • 打赏
  • 举报
回复
哈哈,UP,

TO:
两个表的数据结构不同也可以比较吗?我只是以pd_name为标准而己?

可以,你可以以pd_name为标准,也可以以整个一行为标准,
NOT IN
NOT EXISTS
还有楼上的LEFT JOIN也可以
zhang_yzy 2006-09-06
  • 打赏
  • 举报
回复
select a.*
from a left join b on a.pd_name =b.pd_name
where b.pd_name is null
fjmingyang 2006-09-06
  • 打赏
  • 举报
回复
select * from A where not exists (select 1 from B where pd_name=A.pd_name)
hellowork 2006-09-06
  • 打赏
  • 举报
回复
这样试试:
declare @ta table(id int identity(1,1),pd_name varchar(10))
declare @tb table(id int identity(1,1),pd_name varchar(10))
insert @ta(pd_name)
select 'a' union all
select 'b' union all
select 'c' union all
select 'd' union all
select 'e'
insert @tb(pd_name)
select 'a' union all
select 'e'

select a.* from @ta a
full join @tb b
on a.pd_name = b.pd_name
where b.pd_name is null
子陌红尘 2006-09-06
  • 打赏
  • 举报
回复
两个表的数据结构不同也可以比较吗?我只是以pd_name为标准而己?
----------------------------------------------------------------------------
可以,一楼的SQL即可。
clubsondy 2006-09-06
  • 打赏
  • 举报
回复
两个表的数据结构不同也可以比较吗?我只是以pd_name为标准而己?
wlspeed 2006-09-06
  • 打赏
  • 举报
回复
用not in 也可以 但是效率很低,数据量不大的表就无所谓了。
zsforever 2006-09-06
  • 打赏
  • 举报
回复
select * from A where not exists (select 1 from B where pd_name=A.pd_name)
xiequanqin 2006-09-06
  • 打赏
  • 举报
回复
SELECT A.* FROM A
WHERE pd_name NOT IN(SELECT pd_name FROM B)
clubsondy 2006-09-06
  • 打赏
  • 举报
回复
差不多可以结贴啦。

34,590

社区成员

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

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