现在有A表(主表),结构和数据如下:serial_no xm00000000000194013768 时**
00000000000194005220 孟**
B表(从表),结构和数据如下:serial_no a_serial_no rq12010100050000000143 00000000000194005220
12010100050000000161 00000000000194005220 20070411
12010100050000000145 00000000000194013768 20070410用以下SQL文检索:select a.xm,
b.rq,
b.serial_no
from a,b
where a.serial_no = b.a_serial_no(+)
and b.rq(+) is null ; 结果如下:rownum xm rq serial_no 1 孟** 12010100050000000143
2 时** 可是我预想的结果却是:
rownum xm rq serial_no 1 孟** 12010100050000000143
2 时**
3 孟** 请问为什么我预想中的rownum:3没有出来?
如果条件不符合的话,那为什么rownum:2却能出来,
他们应该是一样的呀?
为什么rownum:1出来后,我预想的rownum:3就被过滤掉了?
00000000000194005220 孟**
B表(从表),结构和数据如下:serial_no a_serial_no rq12010100050000000143 00000000000194005220
12010100050000000161 00000000000194005220 20070411
12010100050000000145 00000000000194013768 20070410用以下SQL文检索:select a.xm,
b.rq,
b.serial_no
from a,b
where a.serial_no = b.a_serial_no(+)
and b.rq(+) is null ; 结果如下:rownum xm rq serial_no 1 孟** 12010100050000000143
2 时** 可是我预想的结果却是:
rownum xm rq serial_no 1 孟** 12010100050000000143
2 时**
3 孟** 请问为什么我预想中的rownum:3没有出来?
如果条件不符合的话,那为什么rownum:2却能出来,
他们应该是一样的呀?
为什么rownum:1出来后,我预想的rownum:3就被过滤掉了?
如果B表中的 [时**] 可以检索出来,
那么B表中的 [孟**,rq=20070411]应该也可以检索出来呀?
and b.rq(+) is null ; 有问题吧!这个+貌似是没用的
去掉这个就出3个结果了吧!
那么B表中的 [孟**,rq=20070411]应该也可以检索出来呀?这个结果的rq is not null
b.rq,
b.serial_no
from a,b
where a.serial_no = b.a_serial_no(+)
and b.rq(+) is null ;
等价于:
select a.xm, b.rq, b.serial_no
from a left join b on a.serial_no = b.a_serial_no and b.rq is null
也就是说a表左连接b表,看b表是否存在a.serial_no = b.a_serial_no and b.rq is null的纪录,如果存在,则显示相关的b表中的信息;否则,对a表中的纪录在b表中找不到纪录相匹配的话,则在查询出的结果集的对应字段中显示null,所以仅仅返回两条纪录,而不是楼主希望的3条纪录
b.rq(+) is null 不是不起作用,也不是b.rq is null
(1)... left join b on ... and b.rq is null
(2)... left join b on ...
where b.rq is null
上面两个查询的结果是不一样的,尽管可以多拿几条记录尝试一下
b.rq,
b.serial_no
from a,b
where a.serial_no = b.a_serial_no(+)
and b.rq(+) is null ; -> and b.rq is null
楼主还是没说这2个语句的结果有什么不同啊!
... and b.cgrq(+) is null 的结果:1 孟** 12010100050000000143
2 时** ... and b.cgrq is null 的结果: 1 孟兆友 12010100050000000143