oracle - 从标准 SQL 到 Oracle 语法的转换

标签 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

甲骨文数据库:

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 相同的结果?

最佳答案

您的 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.

关于oracle - 从标准 SQL 到 Oracle 语法的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21442150/

相关文章:

mysql - 如何在 Laravel 5.4 中进行左连接

mysql - 在左连接查询中获取每个时间戳的最新记录

sql - 如何在MySQL中进行FULL OUTER JOIN?

sql - 通过 postgres 中的连接表对行进行排序

sql - Oracle 12c 数据默认值

oracle - 在Oracle中,我们如何在dbms_aq.dequeue中有条件地使数据出队

sql - 带有多个列的oracle汇总功能

SQL连接问题

php - 列出推荐的 friend

MySQL:涉及两个表和第三个关联表的复杂连接语句