【问题标题】:Conversion from standard SQL to Oracle Syntax从标准 SQL 到 Oracle 语法的转换
【发布时间】:2014-01-29 20:40:18
【问题描述】:

我在将标准 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 相同的结果?

【问题讨论】:

  • 为什么要使用旧语法?我知道你写了不要问但是......使用 ANSI 标准,你不需要转换任何东西。
  • 有些公司(甚至是大公司)“出于历史原因”强制使用旧语法,即使在使用 Oracle 12c 时也是如此。我也不喜欢……
  • 如果您想用FAST REFRESH 创建一个MATERIALIZED VIEW,您必须使用旧的Oracle 语法,ANSI 不起作用。即使在 12c 版本中,Oracle 也不认为这是一个错误,它只是“缺少文档”!!!
  • @Wernfried:有趣!很高兴知道!

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


【解决方案1】:

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

【讨论】:

  • 非常感谢您提供链接!
猜你喜欢
  • 2018-04-24
  • 2020-06-10
  • 1970-01-01
  • 2013-01-14
  • 2019-03-08
  • 2021-11-11
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
相关资源
最近更新 更多