是否使用JOIN?

发布于 2024-12-12 08:12:51 字数 632 浏览 0 评论 0原文

可能的重复:
INNER JOIN 与 WHERE 子句 - 有什么区别吗?
SQL JOIN:USING、在哪里?

例如,我有这个 SQL 语句:

SELECT *  
  FROM orders, inventory  
 WHERE orders.product = inventory.product

或者

SELECT *  
  FROM orders  
  JOIN inventory  
    ON orders.product = inventory.product

这两者有什么区别?

Possible Duplicate:
INNER JOIN versus WHERE clause — any difference?
SQL JOIN: is there a difference between USING, ON or WHERE?

For example, I have this SQL statement:

SELECT *  
  FROM orders, inventory  
 WHERE orders.product = inventory.product

or

SELECT *  
  FROM orders  
  JOIN inventory  
    ON orders.product = inventory.product

What is the difference between these two?

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

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

发布评论

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

评论(4

初雪 2024-12-19 08:12:51

它们做完全相同的事情,但为了可读性和可维护性,我建议使用第二种方法。

  1. 使用 JOIN 允许您将定义表之间关系的条件与结果集上过滤器的条件分开。

  2. 使用 JOIN 可以更轻松地查看是否缺少连接条件。

  3. 使用JOIN可以让您轻松地在INNEROUTER JOIN之间进行选择。逗号语法相当于 INNER JOIN (尽管某些数据库确实具有允许在使用第一种方法时进行外部联接的扩展)。

  4. 最重要的是要保持使用的一致性。逗号语法与 JOIN 关键字的优先级不同,如果您尝试在同一查询中混合使用这两种语法,可能会导致混乱的错误。由于第 3 点,如果始终使用 JOIN,则更容易保持一致。

They do exactly the same thing, but I'd recommend the second approach for readability and maintainability.

  1. Using JOIN allows you to separate the conditions that define relationships between tables from conditions which are filters on the result set.

  2. Using JOIN makes it easier to see if you are missing a join condition.

  3. Using JOIN allows you to easily choose between INNER or OUTER JOIN. The comma syntax is equivalent to INNER JOIN (though some databases do have an extension to allow an outer join when using the first approach).

  4. The most important is to be consistent about which you use. The comma syntax has different precedence from the JOIN keyword which can lead to confusing errors if you try to mix the two syntaxes in the same query. Because of point 3, it is easier to be consistent if you always use JOIN.

何以畏孤独 2024-12-19 08:12:51

内连接是ansi语法,应该是首选方法。

想象一下,如果您将 , 与多个表一起使用,这个解决方案会变得多么丑陋?
SELECT * FROM 订单、库存、产品、物流、会计、材料...

请善待您未来的开发人员以及查看或维护此代码的任何其他人,使用 JOIN句法。

Inner join is ansi syntax, should be the preferred method.

Imagine how ugly this solution could get if you were to use the , with say many tables?
SELECT * FROM orders, inventory, products, logistics, accounting, materials, ...

Be kind to your future developers and anyone else looking at or maintaining this code use the JOIN syntax.

对风讲故事 2024-12-19 08:12:51

逗号 (,) 相当于 CROSS JOIN。使用显式的 CROSS JOIN 更加直观,推荐使用,因为它可以轻松更改为 LEFT JOINRIGHT JOIN 等。使用 < code>CROSS JOIN 也符合 ANSI 标准。

The comma (,) is equivalent to an CROSS JOIN. Using an explicit CROSS JOIN is more intuitive and recommended, as it can easily be changed to a LEFT JOIN, RIGHT JOIN, etc. Using CROSS JOIN is also ANSI-compliant.

往事风中埋 2024-12-19 08:12:51

它们在功能上是等效的。第二个是“较新”的。

They are functionally equivalent. The second one is 'newer'.

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