甲骨文“(”)”操作员
我正在检查一些旧的 SQL 语句,以便记录它们并可能增强它们。
DBMS 是 Oracle。
我不明白这样的声明:
select ...
from a,b
where a.id=b.id(+)
我对 (+)
运算符感到困惑,并且无法在任何论坛上获得它...(在引号内搜索 + 也不起作用)。
无论如何,我使用了 SQLDeveloper 的“解释计划”,并且得到了一个输出,其中显示 HASH JOIN、RIGHT OUTER
等。
如果删除 (+)
会有什么不同吗> 运算符位于查询末尾?在使用 (+)
之前,数据库是否必须满足某些条件(例如有一些索引等)?
I am checking some old SQL Statements for the purpose of documenting them and probably enhancing them.
The DBMS is Oracle.
I did not understand a statement which read like this:
select ...
from a,b
where a.id=b.id(+)
I am confused about the (+)
operator, and could not get it at any forums... (searching for + within quotes didn't work either).
Anyway, I used 'Explain Plan' of SQLDeveloper and I got an output saying that HASH JOIN, RIGHT OUTER
, etc.
Would there be any difference if I remove the (+)
operator at the end of the query? Does the database have to satisfy some condition (like having some indexes, etc.) before (+)
can be used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是 OUTER JOIN 的 Oracle 特定表示法,因为 ANSI-89 格式(在 FROM 子句中使用逗号来分隔表引用)没有标准化 OUTER JOIN。
该查询将以 ANSI-92 语法重写为:
此链接很好地解释了 JOIN 之间的区别。
还应该注意的是,即使
(+)
有效,Oracle 建议不要使用它:That's Oracle specific notation for an OUTER JOIN, because the ANSI-89 format (using a comma in the FROM clause to separate table references) didn't standardize OUTER joins.
The query would be re-written in ANSI-92 syntax as:
This link is pretty good at explaining the difference between JOINs.
It should also be noted that even though the
(+)
works, Oracle recommends not using it:在 Oracle 中,(+) 表示 JOIN 中的“可选”表。因此,在您的查询中,
它是表“b”到表“a”的左外连接。当对方(可选表“b”)没有数据时,它将返回表“a”的所有数据,而不会丢失其数据。
同一查询的现代标准语法将是
or ,带有
a.id=b.id
的简写形式(并非所有数据库都支持):如果删除 (+ )那么这将是正常的内连接查询
Oracle 和其他数据库中的旧语法:
更现代的语法:
或者简单地说:
它只会返回所有包含 'a' 和 'a' 的数据。 'b'表'id'值相同,表示公共部分。
如果您想让查询成为 Right Join
这与 LEFT JOIN 相同,但切换哪个表是可选的。
旧的 Oracle 语法:
现代标准语法:
Ref &帮助:
https://asktom.oracle .com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:6585774577187
在 Oracle 11g 中使用 + 登录进行左外连接
https://www.w3schools.com/sql/sql_join_left.asp
In Oracle, (+) denotes the "optional" table in the JOIN. So in your query,
it's a LEFT OUTER JOIN of table 'b' to table 'a'. It will return all data of table 'a' without losing its data when the other side (optional table 'b') has no data.
The modern standard syntax for the same query would be
or with a shorthand for
a.id=b.id
(not supported by all databases):If you remove (+) then it will be normal inner join query
Older syntax, in both Oracle and other databases:
More modern syntax:
Or simply:
It will only return all data where both 'a' & 'b' tables 'id' value is same, means common part.
If you want to make your query a Right Join
This is just the same as a LEFT JOIN, but switches which table is optional.
Old Oracle syntax:
Modern standard syntax:
Ref & help:
https://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:6585774577187
Left Outer Join using + sign in Oracle 11g
https://www.w3schools.com/sql/sql_join_left.asp
(+) 运算符表示外连接。这意味着即使没有匹配,Oracle 仍将从连接的另一端返回记录。例如,如果 a 和 b 分别是 emp 和 dept,并且您可以将员工未分配到某个部门,则以下语句将返回所有员工的详细信息,无论他们是否已分配到某个部门。
简而言之,删除 (+) 可能会产生显着差异,但根据您的数据,您可能暂时不会注意到!
The (+) operator indicates an outer join. This means that Oracle will still return records from the other side of the join even when there is no match. For example if a and b are emp and dept and you can have employees unassigned to a department then the following statement will return details of all employees whether or not they've been assigned to a department.
So in short, removing the (+) may make a significance difference but you might not notice for a while depending on your data!
实际上,+ 符号直接放置在条件语句中和可选表(允许在条件中包含空值或 null 值的表)一侧。
In practice, the + symbol is placed directly in the conditional statement and on the side of the optional table (the one which is allowed to contain empty or null values within the conditional).