-3

Most RDBMSs support both syntaxes:

SELECT ...
FROM table1 t1 JOIN table2 t2
ON t1.jfield = t2.jfield

as well as

SELECT ...
FROM table1 t1, table2 t2
WHERE t1.jfield = t2.jfield

Personally, I prefer the latter version because of less verbosity but does it presents any difference in performance and query execution? I have seen people on this forum making remarks that the 2nd style is obsolete and does not perform as well.

4

3 回答 3

3

There is no difference in performance. But a lot in robustness. Leave the WHERE clause out in the second example and you wind up with an unwanted cartesian product. Leave out the ON in the first example and you just get a syntax error. That might be obvious for a two table join, but once you have 10 or 15 tables, those errors are hard to spot.

Additionally when you start using outer joins, using LEFT OUTER JOIN will work across all DBMS, whereas any DBMS specific outer join syntax is just that: DBMS specific (and e.g. Oracle's (+) operator can't do everything a LEFT OUTER JOIN can do.

So do get used to using the JOIN keyword.

于 2012-11-08T20:07:22.093 回答
1

Have a look at the execution plans of both forms and you'll see that they are identical (certainly true for MSSQL but I would eat my hat if it isn't for all databases) Since execution plan is identical there will be no performance difference between the 2 forms.

The reason a lot of people (me included) try to rid the world of the 2nd form is because:

  • Joins describe a relationship between 2 tables
  • Where clauses define filters on the result set

Those 2 are functionally very different and using the 2nd form makes it much harder to read and understand queries.

于 2012-11-08T20:07:54.567 回答
0

我建议使用第一种语法。它为更新后的代码提供了很好的代码。第二种语法真的很旧,不能更喜欢这个,因为连接所有来自表的结果和过滤数据之后。也许我错了,但我认为如果你有索引和 PK,你可以通过连接获得更高的性能,因为 SQL Server 可以为这个操作提供更好的支持(例如排序、过滤、子查询等)。Where 子句用于过滤您的数据最终结果。
JOINS是非常强大的工具,因为您可以根据需要仅加入过滤后的数据,而不是加入所有表结果。可能更适合 RAM 或缓存​​。

1st - 您可以添加过滤数据,如下所示的语法 - 下一个语法显示您也可以使用的子查询,它仅连接过滤的行并仅返回该表的特定列
SELECT A., B.
FROM table_a as A
INNER JOIN table_B as B ON B.id = A.id -- join table b
AND B.name = 'bob' AND B.active = 1 -- lets filter more and return only rows for clause



SELECT a., b., c.flag
FROM table_a as a
INNER JOIN table_B as b ON b.id = a.id -- join table b
AND b.name = 'bob' AND b.active = 1 -- lets filter more and return only rows for clause
INNER JOIN (
SELECT id, flag
FROM table_C
WHERE active = 1
) as c ON c.id = a.id


它是一种简单、易读且易于维护的代码。

看看这个链接,它不是漂亮的代码吗?
http://www.dpriver.com/products/sqlpp/sqlexamples4.php

比 WHERE 子句中的 x 条件 ... ofc 你不能在这里使用聚合和子查询 ...我建议了解更多关于连接的信息,你会很高兴程序员 =]
http://www.w3schools.com/sql/ sql_join.asp

希望有帮助

于 2012-11-08T20:42:49.797 回答