我有个需求是这样的:
A B两个表
A和B是一对多外键关系
A的主键是aid同时也是B的外键,B的主键是bid
我现在要取A表中没有在B表出现我的 sql是:
第一种
select a.aid
from A a
where a.aid not in (select aid from B)这个因为A B表数量大,太慢。第二种
select a.aid
from A a,B b
where A.aid=B.aid(+)
and B.bid is null这个好像倒是挺快的,但是我不确定对不。第三种
select a.aid
from A a
where a.aid not in (select aid from B where aid=a.aid)高手指点下。数据量比较大。不知道该怎么写才好呀。
第二种不确定是不是正确呀。查的结果是一样的。
A B两个表
A和B是一对多外键关系
A的主键是aid同时也是B的外键,B的主键是bid
我现在要取A表中没有在B表出现我的 sql是:
第一种
select a.aid
from A a
where a.aid not in (select aid from B)这个因为A B表数量大,太慢。第二种
select a.aid
from A a,B b
where A.aid=B.aid(+)
and B.bid is null这个好像倒是挺快的,但是我不确定对不。第三种
select a.aid
from A a
where a.aid not in (select aid from B where aid=a.aid)高手指点下。数据量比较大。不知道该怎么写才好呀。
第二种不确定是不是正确呀。查的结果是一样的。
解决方案 »
- Oracle 知道入职日期如何知道周几
- oracle 中 用system创建了一个表,退出重新登陆,就找不到表了?
- 全两个表合并的sql
- 如何依据A表的日数据,自动在B表中生产它每天的合计数据?
- 请教Oracle 01652 27042错误
- 国家字符集造成的乱码问题
- ?Oracle在一个用户下面最多可以创建多少个Table?
- 存储过程表名是变量怎么办?
- 误删除不用的ora文件。服务启动不了。请高手解答!在线等。急。。。。。谢谢
- 过程中,怎么取得系统当前的时间,进一不得到年,月,日,请指点,谢谢!
- 大姐求助:我是win2003+oracle9i+eclipse+jboss,内存一个G,现在搞开发,太慢,能否帮我优化一下。
- 帮忙小看一下这个问题~
from A a
where not exists
(select 'x' from b
where a.aid = b.aid )
select count(*) from b where bid is null //记录是0就是对的你这个是主件不会是null
我跑了几次总数都是一致的。
很郁闷呀。第二种写法是我自己乱写的,但是我想左联之后B表没有的数据就是B的id为空了。
minus
select sid from A得到a中没有的sid
左外连接如果不匹配的话,ORACLE会自动赋一个null值。
所以当b.bid=null就找出了在A表而B表中没有的。