Oracle JOIN 3 表 - 哪种方式有效或者有什么区别

发布于 2024-10-15 11:45:03 字数 251 浏览 2 评论 0原文

我可以看到人们以不同的方式进行连接

从a、b、c中选择a.acc、b.acc、c.acc 其中 a.acc=b.acc 且 c.acc = a.acc;

选择 a.acc、b.acc、c.acc 加入 a.acc=b.acc JOIN on c.acc = a.acc;

有什么区别吗?我想不会。

I can see people doing joins in different ways

select a.acc, b.acc, c.acc from a, b,c
where a.acc=b.acc and c.acc = a.acc;

and

select a.acc, b.acc, c.acc JOIN on
a.acc=b.acc JOIN on c.acc = a.acc;

Is there any difference? I suppose not.

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

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

发布评论

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

评论(3

哥,最终变帅啦 2024-10-22 11:45:03

就性能而言,没有区别。

考虑到可维护性,我更喜欢第二种方法。使用哪些条件来连接哪些表更加明确,并且更容易看出您是否错过了连接条件。

In terms of performance there is no difference.

I prefer the second approach for maintainability. It is more explicit which conditions are used to join which tables and it is easier to see whether or not you have missed out a join condition.

夏至、离别 2024-10-22 11:45:03

除了 Mark 指出的使用后一种语法有助于确保您不会无意中错过连接条件之外,SQL 99 语法的一个好处是,如果您的列命名约定是父级和子级中的列名称表匹配时,可以使用 USING 子句而不是 ON 子句,即

SELECT <<list of columns>>
  FROM a JOIN b USING( acc )
         JOIN c USING( acc )

这样可以提高代码的可读性,并减少因无意中错误连接表而引入错误的可能性。如果每次我不小心写出一些像

SELECT <<list of columns>>
  FROM a,
       a_to_b,
       b
 WHERE a.a_id = a_to_b.a_id
   AND b.b_id = a_to_b.a_id -- a_to_b.**a_id** rather than a_to_b.**b_id**

我真正意味着

SELECT <<list of columns>>
  FROM a,
       a_to_b,
       b
 WHERE a.a_id = a_to_b.a_id
   AND b.b_id = a_to_b.b_id

我会成为一个有钱人的东西时我都会得到五分钱。或者至少有足够的寿司晚餐。

当然,大多数时候,很明显你做错了什么,因为数据完全是错误的,但有时结果足够接近,你并不能立即明显看出你做错了什么,直到很久以后,您发现了错误并追踪了罪魁祸首的查询。如果使用 USING 子句编写查询,基本上不可能犯这种错误

SELECT <<list of columns>>
  FROM a JOIN a_to_b USING (a_id)
         JOIN b      USING (b_id)

In addition to Mark's point that using the latter syntax helps ensure that you don't inadvertently miss a join condition, one nice thing about the SQL 99 syntax is that if your column naming convention is that the name of the column in the parent and child table matches, you can use the USING clause rather than the ON clause, i.e.

SELECT <<list of columns>>
  FROM a JOIN b USING( acc )
         JOIN c USING( acc )

This can improve the readability of the code and decrease the probability that you introduce errors by inadvertently joining tables incorrectly. If I had a nickel for every time I accidentally wrote something like

SELECT <<list of columns>>
  FROM a,
       a_to_b,
       b
 WHERE a.a_id = a_to_b.a_id
   AND b.b_id = a_to_b.a_id -- a_to_b.**a_id** rather than a_to_b.**b_id**

when I really meant

SELECT <<list of columns>>
  FROM a,
       a_to_b,
       b
 WHERE a.a_id = a_to_b.a_id
   AND b.b_id = a_to_b.b_id

I'd be a rich man. Or at least have enough for a good sushi dinner.

Most of the time, of course, it's immediately obvious that you've done something wrong because the data is completely screwy but it occasionally happens that the results are sufficiently close that it's not immediately obvious that you've done something wrong and it's not until much later that you discover the bug and track down the culprit query. It's basically impossible to make that sort of mistake if you write the query with the USING clause

SELECT <<list of columns>>
  FROM a JOIN a_to_b USING (a_id)
         JOIN b      USING (b_id)
又爬满兰若 2024-10-22 11:45:03

如果您的第二个选择是读取 select a.acc, b.acc, c.acc from a JOIN b on a.acc=b.acc JOIN c on c.acc = a.acc; 那么没有什么区别。

if your second select is to read select a.acc, b.acc, c.acc from a JOIN b on a.acc=b.acc JOIN c on c.acc = a.acc; then there is no difference.

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