用于外连接 (+) 的旧 Oracle 语法是否始终等同于新语法?
我想知道是否总是可以使用旧的 Oracle 语法(+)
重写LEFT JOIN
。特别是,我尝试使用 (+)
表达以下查询,
SELECT *
FROM table_1 a
LEFT JOIN table_2 b ON (b.table1_id = a.id AND b.other_field = 'value1')
但没有成功。有可能吗?
谢谢。
I wonder if it's always possible to rewrite LEFT JOIN
using old Oracle syntax(+)
. In particular, I tried to express the following query using (+)
SELECT *
FROM table_1 a
LEFT JOIN table_2 b ON (b.table1_id = a.id AND b.other_field = 'value1')
without success. Is it possible at all?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜您在 b.other_field 的测试中没有使用 (+) 运算符。
这应该有效:
I'm guessing you're not using the (+) operator in the test of b.other_field..
This should work:
如果我没记错的话,用 Oracle 旧的外连接语法重写 ANSI 连接并不总是可能的,因为执行顺序可能会更改返回的行。
“没有成功”是什么意思?您收到错误了吗?您是否得到了错误的行?您是否得到了错误的列?
左连接将保留 table_1 中的所有行。旧式 Oracle 语法的基本形式是带有 WHERE 子句的笛卡尔积,以及另一个表上的“+”标记。 (这不包括整个 WHERE 子句。这是故意的。)
例如,请参见 询问汤姆。
用于故障排除。 。 。
如果您从查询开始
并消除了别名,那么您
实际上会看到名为
table_2.table1_id
和table_1.id
的列吗?那有用吗?如果这不是问题,那就从更简单的开始。试试这个。
那有用吗?接下来试试这个。
如果有效,请尝试添加 JOIN 子句的其余部分。
If I recall correctly, it's not always possible to rewrite an ANSI join in Oracle's old outer join syntax, because the order of execution can change the rows returned.
What does "without success" mean? Did you get an error? Did you get the wrong rows? Did you get the wrong columns?
A left join will preserve all the rows in table_1. The basic form of old-style Oracle syntax is a Cartesian product with a WHERE clause, and a "+" token on the other table. (This doesn't include your entire WHERE clause. That's deliberate.)
See, for example, AskTom.
For troubleshooting . . .
If you start with your query
and eliminated the aliases, you'd have
Are there actually columns named
table_2.table1_id
andtable_1.id
? Does that work?If that's not the problem start simpler. Try this.
Does that work? Next try this.
If that works, try adding the rest of your JOIN clause.