甲骨文“(”)”操作员

发布于 2024-09-28 17:15:51 字数 403 浏览 7 评论 0原文

我正在检查一些旧的 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 技术交流群。

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

发布评论

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

评论(4

尘世孤行 2024-10-05 17:15:51

这是 OUTER JOIN 的 Oracle 特定表示法,因为 ANSI-89 格式(在 FROM 子句中使用逗号来分隔表引用)没有标准化 OUTER JOIN。

该查询将以 ANSI-92 语法重写为:

   SELECT ...
     FROM a
LEFT JOIN b ON b.id = a.id

此链接很好地解释了 JOIN 之间的区别


还应该注意的是,即使 (+) 有效,Oracle 建议不要使用它

Oracle 建议您使用 FROM 子句 OUTER JOIN 语法而不是 Oracle 连接运算符。使用 Oracle 连接运算符 (+) 的外连接查询受以下规则和限制的约束,这些规则和限制不适用于 FROM 子句 OUTER JOIN语法:


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:

   SELECT ...
     FROM a
LEFT JOIN b ON b.id = a.id

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 recommends that you use the FROM clause OUTER JOIN syntax rather than the Oracle join operator. Outer join queries that use the Oracle join operator (+) are subject to the following rules and restrictions, which do not apply to the FROM clause OUTER JOIN syntax:

临走之时 2024-10-05 17:15:51

在 Oracle 中,(+) 表示 JOIN 中的“可选”表。因此,在您的查询中,

SELECT a.id, b.id, a.col_2, b.col_2, ...
FROM a,b
WHERE a.id=b.id(+)

它是表“b”到表“a”的左外连接。当对方(可选表“b”)没有数据时,它将返回表“a”的所有数据,而不会丢失其数据。

左图外部连接

同一查询的现代标准语法将是

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a
LEFT JOIN b ON a.id=b.id

or ,带有 a.id=b.id 的简写形式(并非所有数据库都支持):

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a
LEFT JOIN b USING(id)

如果删除 (+ )那么这将是正常的内连接查询

Oracle 和其他数据库中的旧语法:

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a,b
WHERE a.id=b.id

更现代的语法:

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a
INNER JOIN b ON a.id=b.id

或者简单地说:

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a
JOIN b ON a.id=b.id

内连接图

它只会返回所有包含 'a' 和 'a' 的数据。 'b'表'id'值相同,表示公共部分。

如果您想让查询成为 Right Join

这与 LEFT JOIN 相同,但切换哪个表是可选的。

右图Outer Join

旧的 Oracle 语法:

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a,b
WHERE a.id(+)=b.id

现代标准语法:

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a
RIGHT JOIN b ON a.id=b.id

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,

SELECT a.id, b.id, a.col_2, b.col_2, ...
FROM a,b
WHERE a.id=b.id(+)

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.

Diagram of Left Outer Join

The modern standard syntax for the same query would be

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a
LEFT JOIN b ON a.id=b.id

or with a shorthand for a.id=b.id (not supported by all databases):

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a
LEFT JOIN b USING(id)

If you remove (+) then it will be normal inner join query

Older syntax, in both Oracle and other databases:

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a,b
WHERE a.id=b.id

More modern syntax:

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a
INNER JOIN b ON a.id=b.id

Or simply:

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a
JOIN b ON a.id=b.id

Diagram of Inner Join

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.

Diagram of Right Outer Join

Old Oracle syntax:

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a,b
WHERE a.id(+)=b.id

Modern standard syntax:

SELECT  a.id, b.id, a.col_2, b.col_2, ...
FROM a
RIGHT JOIN b ON a.id=b.id

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

我纯我任性 2024-10-05 17:15:51

(+) 运算符表示外连接。这意味着即使没有匹配,Oracle 仍将从连接的另一端返回记录。例如,如果 a 和 b 分别是 emp 和 dept,并且您可以将员工未分配到某个部门,则以下语句将返回所有员工的详细信息,无论他们是否已分配到某个部门。

select * from emp, dept where emp.dept_id=dept.dept_id(+)

简而言之,删除 (+) 可能会产生显着差异,但根据您的数据,您可能暂时不会注意到!

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.

select * from emp, dept where emp.dept_id=dept.dept_id(+)

So in short, removing the (+) may make a significance difference but you might not notice for a while depending on your data!

伤感在游骋 2024-10-05 17:15:51

实际上,+ 符号直接放置在条件语句中和可选表(允许在条件中包含空值或 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).

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