求两张表查询的语句

wwjlucky 2009-08-03 08:57:49
两张表abl、tbl,结构相同(A、B、C、D列),各有大约170万条数据,有何语句能查出这两张表的A、B两列不相同的条目?
...全文
122 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwjlucky 2009-08-04
  • 打赏
  • 举报
回复
谢谢各位。
zjybushiren88888 2009-08-03
  • 打赏
  • 举报
回复
select * from abl t where not exists(select 1 from tbl where a=t.a and b=t.b)
union all
select * from tbl t where not exists(select 1 from abl where a=t.a and b=t.b)
zwzw911 2009-08-03
  • 打赏
  • 举报
回复
select a.* from a inner join b on a.a<>b.a and a.b<>b.b
union all
select b.* from b inner join a on a.a<>b.a and a.b<>b.b

不知道inner join和not exists,哪个效率比较高?
zjb211434 2009-08-03
  • 打赏
  • 举报
回复
--将上面的方法两句合并成一句
declare @ab1 table(A int, b int, c int, d int)
insert @ab1 select
1,2,3,4 union all select
2,3,4,5 union all select
3,4,5,6
declare @ab2 table(A int, b int, c int, d int)
insert @ab2 select
1,2,3,4 union all select
2,3,4,5 union all select
4,5,6,7


select case when a.A is null then b.A else a.A end,
case when a.A is null then b.B else a.B end,
case when a.A is null then b.C else a.C end,
case when a.A is null then b.D else a.D end
from @ab1 a full outer join @ab2 b on a.A=b.A and a.B=b.B and a.C=b.C and a.D=b.D
where a.A is null or b.A is null
zjb211434 2009-08-03
  • 打赏
  • 举报
回复
declare @ab1 table(A int, b int, c int, d int)
insert @ab1 select
1,2,3,4 union all select
2,3,4,5 union all select
3,4,5,6
declare @ab2 table(A int, b int, c int, d int)
insert @ab2 select
1,2,3,4 union all select
2,3,4,5 union all select
4,5,6,7

select a.A, a.B, a.C, a.D from @ab1 a full outer join
@ab2 b on a.A=b.A and a.B=b.B and a.C=b.C and a.D=b.D where b.A is null
union all
select b.A, b.B, b.C, b.D from @ab1 a full outer join
@ab2 b on a.A=b.A and a.B=b.B and a.C=b.C and a.D=b.D where a.A is null

--这个方法是能实现,不过不知道效率怎么样,关注大牛们有没有高效的方法
soft_wsx 2009-08-03
  • 打赏
  • 举报
回复

--A表
select * from abl t where not exists(select 1 from tbl where a=t.a and b=t.b)
union all
--B表
select * from tbl t where not exists(select 1 from abl where a=t.a and b=t.b)
htl258_Tony 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 htl258 的回复:]
SQL codeselect*from (select*from ablexceptselect*from tbl
)as aunionallselect*from (select*from tblexceptselect*from abl
)as b2005
[/Quote]
不好意思,只是AB两列不是这样,用楼上的会简单点.
htl258_Tony 2009-08-03
  • 打赏
  • 举报
回复
select *
from (
select * from abl
except
select * from tbl
) as a
union all
select *
from (
select * from tbl
except
select * from abl
) as b
2005
SQL77 2009-08-03
  • 打赏
  • 举报
回复
SELECT COUNT(*) FROM A WHERE NOT EXISTS(SELECT 1 FROM B WHERE A.A=B.A AND A.B=B.B)
UNION ALL
SELECT COUNT(*) FROM B WHERE NOT EXISTS(SELECT 1 FROM A WHERE A.A=B.A AND A.B=B.B)
SQL77 2009-08-03
  • 打赏
  • 举报
回复
SELECT * FROM A WHERE NOT EXISTS(SELECT 1 FROM B WHERE A.A=B.A AND A.B=B.B)
UNION ALL
SELECT * FROM B WHERE NOT EXISTS(SELECT 1 FROM A WHERE A.A=B.A AND A.B=B.B)
??
水族杰纶 2009-08-03
  • 打赏
  • 举报
回复
select * from abl t where not exists(select 1 from tbl where a=t.a and b=t.b)
union all
select * from tbl t where not exists(select 1 from abl where a=t.a and b=t.b)
zhaoyh0530 2009-08-03
  • 打赏
  • 举报
回复
求ab1中不在tb1的并上tb1中不在ab1中的:
select *
from abl t
where not exists(select 1 from tbl where a=t.a and b=t.b)
union all
select *
from tbl t
where not exists(select 1 from abl where a=t.a and b=t.b)
wft001 2009-08-03
  • 打赏
  • 举报
回复
不相等表数为 sum
sum =(select count(*) from(select * from ab1 union select * from tb1) a)-170万
zhengduan964532 2009-08-03
  • 打赏
  • 举报
回复
执行效率如何?
振乾 2009-08-03
  • 打赏
  • 举报
回复
select * from @ab1 t where not exists(select 1 from @ab2 where a=t.a and b=t.b)
union all
select * from @ab2 t where not exists(select 1 from @ab1 where a=t.a and b=t.b)

34,576

社区成员

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

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