如何去除筛选的表中不用的数据

gdl222 2009-04-13 10:40:30
我有两个数据表,想查询中所有表a中不等于表b的数据,
若利用:表a left outer join 表b on...那么结果是表a中所有的数据都会显示出,即使等于表b的部分,后续查询表b的字段为null.
若利用:表a inner join 表b on...那么只能选择表a和表b都符合条件的数据,但是若b的数据为null,我要的结果是表b的数据为空时,也要显示表a的数据,但是若这样设置的话不能显示表b数据为null的情况
请问有什么好办法让我在得到的结果表中:
去除表a中不等于表b的数据,并且若表b某些数据为null,也要带出表a的数据,连接的表b的字段为nuLl即可
...全文
108 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
htl258_Tony 2009-04-13
  • 打赏
  • 举报
回复
set nocount on
if object_id('表1') is not null
drop table 表1
go
create table 表1([ID] int,[NUM] int)
insert 表1 select '1',2
insert 表1 select '2',3
insert 表1 select '3',4
insert 表1 select '4',5
go
if object_id('表2') is not null
drop table 表2
go
create table 表2([ID] int,[num] int)
insert 表2 select '1',2
insert 表2 select '2',4
insert 表2 select '3',5
insert 表2 select '5',6
go
set nocount off

select
a.id,
a.num as num1,
b.num as num2
from (select * from 表1 t where not exists(select 1 from 表2 where id=t.id and num=t.num)) a
left join (select * from 表2 t where not exists(select 1 from 表1 where id=t.id and num=t.num)) b
on a.id=b.id and a.num<>b.num

/*
id num1 num2
----------- ----------- -----------
2 3 4
3 4 5
4 5 NULL

(3 行受影响)
*/
百年树人 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 josy 的回复:]
SQL codeselect
a.id,
a.num as num1,
b.num as num2
from 表1 a
left join 表2 b
on a.id=b.id and a.num<>b.num
[/Quote]
修正一下:

if object_id('[表1]') is not null drop table [表1]
go
create table [表1]([ID] int,[NUM] int)
insert [表1]
select 1,2 union all
select 2,3 union all
select 3,4 union all
select 4,5
if object_id('[表2]') is not null drop table [表2]
go
create table [表2]([ID] int,[num] int)
insert [表2]
select 1,2 union all
select 2,4 union all
select 3,5 union all
select 5,6

select
a.id,
a.num as num1,
b.num as num2
from 表1 a
left join 表2 b
on a.id=b.id
where not exists(select * from 表2 where id=a.id and num=a.num)


--测试结果:
/*
id num1 num2
----------- ----------- -----------
2 3 4
3 4 5
4 5 NULL

(所影响的行数为 3 行)
*/
百年树人 2009-04-13
  • 打赏
  • 举报
回复
select 
a.id,
a.num as num1,
b.num as num2
from 表1 a
left join 表2 b
on a.id=b.id and a.num<>b.num
gdl222 2009-04-13
  • 打赏
  • 举报
回复
表1:
ID NUM
1 2
2 3
3 4
4 5
.. ..



表2:
ID num
1 2
2 4
3 5
5 6
.. ..


需要得到的结果表:
ID num1 num2
2 3 4
3 4 5
4 5 null
.. .. ..


说明:
若表1的ID与表2的ID相同,且数量不同,可以显示结果表3的(2,3,4)(3,4,5)的情况;
若表1的ID num与表2的ID num相同,不显示;
若表1的ID有,表2的ID没有,只显示表1的ID num,且表2的num为Null
若表2的id有,表1的id没有,不显示


请各位高手帮忙设计下查询的语言?
-晴天 2009-04-13
  • 打赏
  • 举报
回复
--如果两表的比较都用id列来进行:
select * from tbA where not exists(select 1 from tbB where id=a.id)
但,null值是没法比较的.
htl258_Tony 2009-04-13
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090413/22/62bb6f2b-ae0a-458d-bde3-014413d9aa2c.html

这是有人刚提问题,里面有涉及到相关的,你不防看看。
-晴天 2009-04-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 gdl222 的帖子:]
去除表a中不等于表b的数据,并且若表b某些数据为null,也要带出表a的数据,连接的表b的字段为nuLl即可
[/Quote]
既然表b的数据为null,如何还能带出表a的数据?以什么来比较?
-狙击手- 2009-04-13
  • 打赏
  • 举报
回复
看看JJ
htl258_Tony 2009-04-13
  • 打赏
  • 举报
回复
写那么多,不如贴出测试数据和期望得到的结果。
jinjazz 2009-04-13
  • 打赏
  • 举报
回复
这个问题用left join 和isnull应该可以解决,你有无测试数据

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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