从标准SQL转换为Oracle语法

时间:2014-01-29 20:40:18

标签: oracle left-join outer-join ansi-sql

我将标准SQL连接转换为旧的Oracle连接语法时遇到问题(请不要问我为什么需要它)。我希望得到相同的结果,不管怎样,它不是:

示例数据:

create table testing (
aid number(8),
bid number(8),
btext varchar(80));

insert into testing values (100,1,'text1a');
insert into testing values (100,2,'text1b');
insert into testing values (100,3,'text1c');
insert into testing values (200,19,'text2b');
insert into testing values (200,18,'text2a');
insert into testing values (300,4324,'text3a');
insert into testing values (500,80,'text4a');
insert into testing values (50,2000,'text5a');
commit;

标准SQL:

select a.*,b.* from testing a
left outer join testing b
on (a.aid = b.aid and a.bid < b.bid)
order by a.aid, b.bid;

AID BID BTEXT   AID_1   BID_1   BTEXT_1
50  200 text5a  NULL    NULL    NULL
100 1   text1a  100     2       text1b
100 2   text1b  100     3       text1c
100 1   text1a  100     3       text1c
100 3   text1c  NULL    NULL    NULL
200 18  text2a  200     19      text2b
200 19  text2b  NULL    NULL    NULL
300 432 text3a  NULL    NULL    NULL
500 80  text4a  NULL    NULL    NULL

Oracle SQL:

select a.*,b.* from testing a, testing b
where a.aid = b.aid(+) 
and a.bid < b.bid
order by a.aid, b.bid;

AID BID BTEXT   AID_1   BID_1   BTEXT_1
100 1   text1a  100     2       text1b
100 2   text1b  100     3       text1c
100 1   text1a  100     3       text1c
200 18  text2a  200     19      text2b

如何使用Oracle的遗留语法获得标准SQL的相同结果?

1 个答案:

答案 0 :(得分:3)

您的Oracle风格语句也需要(+)运算符在小于条件的情况下,因为这也是标准SQL版本中的连接条件的一部分。

select a.*,b.* from testing a, testing b
where a.aid = b.aid(+) 
and a.bid < b.bid(+)
order by a.aid, b.bid;

See sqlfiddle here.