Oracle 外连接简写带上

发布于 2024-09-09 01:05:00 字数 231 浏览 6 评论 0原文

有效:

AND UPPER(a.name) = b.lname(+)

无效

AND UPPER(a.name) = UPPER(b.lname) (+)

迁移到 ANSI 连接是一种选择,但却是一项艰苦的选择。这段代码有很多地方需要修改,并且有很多连接。我想让这个语法正确并继续前进。

是否可以?

Works:

AND UPPER(a.name) = b.lname(+)

does not work

AND UPPER(a.name) = UPPER(b.lname) (+)

Moving to ANSI joins is an option but a painstaking one. This code should be changed in lot of places and there are lot of joins. I would like to get this syntax correct and be on my way.

Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万劫不复 2024-09-16 01:05:00

撇开恶心不谈,将 UPPER() 与旧的 skool OUTER JOIN 语法合并本身很简单:我们只需要以正确的顺序获取括号:

SQL> select t23.name
  2         , d.university
  3  from t23
  4       , t_doctors d
  5  where upper(t23.name) = upper(d.name(+))
  6  order by t23.id
  7  /

NAME         UNIVERSITY
------------ --------------------
SAM-I-AM
MR KNOX
FOX IN SOCKS
CAT
LORAX
BILLY
MAISIE
DR SINATRA   Whoville U
DR FONZ      U of Grin-itch
PINNER BLINN

10 rows selected.

SQL> 

以下是如何使用多个表部署新语法:

SQL> select t23.name
  2         , d.university
  3         , k.school
  4  from t23
  5       left outer join t_doctors d
  6                  on upper(t23.name) = upper(d.name)
  7       left outer join t_kids k
  8                  on upper(t23.name) = upper(k.name)
  9  order by t23.id
 10  /

NAME         UNIVERSITY           SCHOOL
------------ -------------------- --------------------
SAM-I-AM                          Mulberry St Junior
MR KNOX
FOX IN SOCKS
CAT
LORAX
BILLY                             Roover River High
MAISIE                            Roover River High
DR SINATRA   Whoville U
DR FONZ      U of Grin-itch
PINNER BLINN

10 rows selected.

SQL>

Yuckiness aside, incorporating UPPER() with the old skool OUTER JOIN syntax is simplicity itself: we just need to get the brackets in the right order:

SQL> select t23.name
  2         , d.university
  3  from t23
  4       , t_doctors d
  5  where upper(t23.name) = upper(d.name(+))
  6  order by t23.id
  7  /

NAME         UNIVERSITY
------------ --------------------
SAM-I-AM
MR KNOX
FOX IN SOCKS
CAT
LORAX
BILLY
MAISIE
DR SINATRA   Whoville U
DR FONZ      U of Grin-itch
PINNER BLINN

10 rows selected.

SQL> 

Here is how to deploy the newer syntax with multiple tables:

SQL> select t23.name
  2         , d.university
  3         , k.school
  4  from t23
  5       left outer join t_doctors d
  6                  on upper(t23.name) = upper(d.name)
  7       left outer join t_kids k
  8                  on upper(t23.name) = upper(k.name)
  9  order by t23.id
 10  /

NAME         UNIVERSITY           SCHOOL
------------ -------------------- --------------------
SAM-I-AM                          Mulberry St Junior
MR KNOX
FOX IN SOCKS
CAT
LORAX
BILLY                             Roover River High
MAISIE                            Roover River High
DR SINATRA   Whoville U
DR FONZ      U of Grin-itch
PINNER BLINN

10 rows selected.

SQL>
完美的未来在梦里 2024-09-16 01:05:00

第二个版本的代码很可能永远无法工作。如果之前没有使用过,那么它就更可能不起作用。(有关如何使用过时的符号,请参阅已接受的答案。我仍然认为下面的其余建议仍然有效- 但请仔细注意限定符“当您需要修改 SQL 时”;如果您因其他原因不需要更改 SQL,则无需删除旧式表示法。)

当您需要进行不区分大小写的比较时,硬着头皮处理 ANSI 连接。或者研究基于区域设置的替代方案(不区分大小写的比较)(如果 Oracle 中存在这样的选项)。

但从根本上来说,您应该将旧的“(+)”外连接符号扔进垃圾桶。当您必须修改 SQL 语句时,请删除旧的(过时的)表示法并使用 ANSI 连接表示法。


有评论问“如何将其转换为 ANSI”?

您重写 FROM 子句以及 WHERE 子句 - 通常将连接条件从 WHERE 子句移至 FROM 子句中的 ON 条件。

 SELECT a.*, b.*
   FROM a LEFT OUTER JOIN b ON UPPER(a.name) = UPPER(b.lname)

另一个评论问“如何将联接扩展到三个表”?

 SELECT a.*, b.*
   FROM a 
     LEFT OUTER JOIN b ON UPPER(a.name) = UPPER(b.lname)
     LEFT OUTER JOIN c ON on a.first = c.first

It is quite possible that the second version of the code will not work, ever. If it wasn't in use before, it is all the more plausible that it won't work. (See the accepted answer for how to use the obsolesent notation. I still think that the rest of the advice below stands - but note carefully the qualifier 'when you need to modify the SQL'; if you don't need to change the SQL for some other reason, there's no necessity to remove the old-style notation.)

Bite the bullet and deal with the ANSI join when you need to do the case-insensitive comparison. Or investigate a locale-based alternative (with case-insensitive comparisons), if such an option exists in Oracle.

Fundamentally, though, you should consign the old '(+)' outer join notation to the trash can. When you have to modify an SQL statement, remove the old (obsolescent) notation and use the ANSI join notation instead.


A comment asks 'how can this be converted to ANSI'?

You rewrite the FROM clause as well as the WHERE clause - often moving join conditions from the WHERE clause to the ON conditions in the FROM clause.

 SELECT a.*, b.*
   FROM a LEFT OUTER JOIN b ON UPPER(a.name) = UPPER(b.lname)

Another comment asks 'how to extend the join to three tables'?

 SELECT a.*, b.*
   FROM a 
     LEFT OUTER JOIN b ON UPPER(a.name) = UPPER(b.lname)
     LEFT OUTER JOIN c ON on a.first = c.first
清风无影 2024-09-16 01:05:00
AND UPPER(a.name) = UPPER(b.lname (+))              

作品。我测试过。它运行良好。

AND UPPER(a.name) = UPPER(b.lname (+))              

Works. I tested it. It is working fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文