如何在一个表里查询两批数据量大的不同数据记录?

zsp0010 2007-12-05 11:11:49
一个表里导入了两批数据,根据batch这个字段来确定他们的批次。
我现在想通过c1和c2这两个字段比较确定这个表里发生了变化的数据记录。
两批数据通过c1这个字段关联,根据c2字段的变化来确定这条记录是否变化。
每批数据c1有重复值,我通过下面的语句能找出两批数据相同的记录:
select * from Table where c1 in
(
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2<>b.c2 )
)
这个结果是正确的;

但我改成下面的语句(把in改成not in)想查询不同的记录:
select * from Table where c1 not in
(
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2<>b.c2 )
)
结果总是出不来

请问这个问题改怎么解决?

在下不胜感激了!
...全文
200 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Croatia 2007-12-07
  • 打赏
  • 举报
回复
你的batch1 和 batch2里面,是不是存在同样的c1,但是出现多条C2,
batch1和batch2的c2之间,既有相同的,又有不同的?

给出一部分数据看看呢?
zsp0010 2007-12-07
  • 打赏
  • 举报
回复
-----------------------------------------
不好意思,是我把SQL搞反了
我的意思是这样
select * from Table where c1 not in
(
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2 = b.c2 )
)
找出不同的数据的话很难查出来
a.c2 = b.c2
Croatia 2007-12-06
  • 打赏
  • 举报
回复
还有LZ写出来的SQL和自己想要表达的意思,是不是相反了?

〉我通过下面的语句能找出两批数据相同的记录:
〉select * from Table where c1 in
〉(
〉 select distinct a.c1 from
〉 (select distinct c1,c2 from t_Table where batch='1' ) a,
〉 (select distinct c1,c2 from t_Table where batch='2' ) b
〉 where (a.c1=b.c1 and a.c2 <> b.c2 )
〉)
这个时候,你得到的是两批数据相同的数据?


你的batch1 和 batch2里面,是不是存在同样的c1,但是出现多条C2,
batch1和batch2的c2之间,既有相同的,又有不同的?

我看你好像迷惑的是这里。

那这个时候,你又想要什么呢?
Croatia 2007-12-06
  • 打赏
  • 举报
回复
第一个要求:
select * from Table where c1 in
(
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2 <> b.c2 )
)

第二个要求:
select * from Table where c1 in
(
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2 = b.c2 )
)
zsp0010 2007-12-06
  • 打赏
  • 举报
回复
上面的是有问题,我主要想问的是用not in
如果后面的数据量很大,用not in查询起来就很慢
怎么换个语句来解决这个问题?
HelloWorld_001 2007-12-05
  • 打赏
  • 举报
回复
首先看
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2 <> b.c2 )
出来的结果

select * from Table where c1 not in (上面的结果集);
你就发现你写的sql不是你想要的结果了
guhong138 2007-12-05
  • 打赏
  • 举报
回复
提示什么????
有时候加了distinct 返回的值也不一定就是唯一的,如果是这样的话,试着用rowid为条件试一试
zsp0010 2007-12-05
  • 打赏
  • 举报
回复
那请问我这有什么问题,该怎么解决?

17,382

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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