是否使用JOIN?
例如,我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它们做完全相同的事情,但为了可读性和可维护性,我建议使用第二种方法。
使用
JOIN
允许您将定义表之间关系的条件与结果集上过滤器的条件分开。使用
JOIN
可以更轻松地查看是否缺少连接条件。使用
JOIN
可以让您轻松地在INNER
或OUTER JOIN
之间进行选择。逗号语法相当于INNER JOIN
(尽管某些数据库确实具有允许在使用第一种方法时进行外部联接的扩展)。最重要的是要保持使用的一致性。逗号语法与
JOIN
关键字的优先级不同,如果您尝试在同一查询中混合使用这两种语法,可能会导致混乱的错误。由于第 3 点,如果始终使用JOIN
,则更容易保持一致。They do exactly the same thing, but I'd recommend the second approach for readability and maintainability.
Using
JOIN
allows you to separate the conditions that define relationships between tables from conditions which are filters on the result set.Using
JOIN
makes it easier to see if you are missing a join condition.Using
JOIN
allows you to easily choose betweenINNER
orOUTER JOIN
. The comma syntax is equivalent toINNER JOIN
(though some databases do have an extension to allow an outer join when using the first approach).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 useJOIN
.内连接是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.逗号 (
,
) 相当于CROSS JOIN
。使用显式的CROSS JOIN
更加直观,推荐使用,因为它可以轻松更改为LEFT JOIN
、RIGHT JOIN
等。使用 < code>CROSS JOIN 也符合 ANSI 标准。The comma (
,
) is equivalent to anCROSS JOIN
. Using an explicitCROSS JOIN
is more intuitive and recommended, as it can easily be changed to aLEFT JOIN
,RIGHT JOIN
, etc. UsingCROSS JOIN
is also ANSI-compliant.它们在功能上是等效的。第二个是“较新”的。
They are functionally equivalent. The second one is 'newer'.