用于外连接 (+) 的旧 Oracle 语法是否始终等同于新语法?

发布于 2024-12-12 02:56:11 字数 258 浏览 0 评论 0原文

我想知道是否总是可以使用旧的 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 技术交流群。

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

发布评论

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

评论(2

梦言归人 2024-12-19 02:56:11

我猜您在 b.other_field 的测试中没有使用 (+) 运算符。
这应该有效:

SELECT *
FROM table_1 a, table_2 b 
WHERE b.table1_id(+) = a.id 
AND b.other_field(+) = 'value1'

I'm guessing you're not using the (+) operator in the test of b.other_field..
This should work:

SELECT *
FROM table_1 a, table_2 b 
WHERE b.table1_id(+) = a.id 
AND b.other_field(+) = 'value1'
十六岁半 2024-12-19 02:56:11

如果我没记错的话,用 Oracle 旧的外连接语法重写 ANSI 连接并不总是可能的,因为执行顺序可能会更改返回的行。

“没有成功”是什么意思?您收到错误了吗?您是否得到了错误的行?您是否得到了错误的列?

左连接将保留 table_1 中的所有行。旧式 Oracle 语法的基本形式是带有 WHERE 子句的笛卡尔积,以及另一个表上的“+”标记。 (这不包括整个 WHERE 子句。这是故意的。)

SELECT *
FROM table_1 a, table_2 b
WHERE a.id = b.table1_id(+)

例如,请参见 询问汤姆

用于故障排除。 。 。

如果您从查询开始

SELECT *
FROM table_1 a
LEFT JOIN table_2 b ON (b.table1_id = a.id AND b.other_field = 'value1')

并消除了别名,那么您

SELECT *
FROM table_1
LEFT JOIN table_2 ON (table_2.table1_id = table_1.id AND 
                      table_2.other_field = 'value1')

实际上会看到名为 table_2.table1_idtable_1.id 的列吗?那有用吗?

如果这不是问题,那就从更简单的开始。试试这个。

SELECT table_1.id, table_2.table1_id
FROM table_1
INNER JOIN table_2 ON (table_2.table1_id = table_1.id);

那有用吗?接下来试试这个。

SELECT table_1.id, table_2.table1_id
FROM table_1
LEFT JOIN table_2 ON (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.)

SELECT *
FROM table_1 a, table_2 b
WHERE a.id = b.table1_id(+)

See, for example, AskTom.

For troubleshooting . . .

If you start with your query

SELECT *
FROM table_1 a
LEFT JOIN table_2 b ON (b.table1_id = a.id AND b.other_field = 'value1')

and eliminated the aliases, you'd have

SELECT *
FROM table_1
LEFT JOIN table_2 ON (table_2.table1_id = table_1.id AND 
                      table_2.other_field = 'value1')

Are there actually columns named table_2.table1_id and table_1.id? Does that work?

If that's not the problem start simpler. Try this.

SELECT table_1.id, table_2.table1_id
FROM table_1
INNER JOIN table_2 ON (table_2.table1_id = table_1.id);

Does that work? Next try this.

SELECT table_1.id, table_2.table1_id
FROM table_1
LEFT JOIN table_2 ON (table_2.table1_id = table_1.id);

If that works, try adding the rest of your JOIN clause.

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