SQL INNER JOIN 与“FROM”中的多个表名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5118562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
INNER JOIN vs multiple table names in "FROM"
提问by Michael
Possible Duplicate:
INNER JOIN versus WHERE clause — any difference?
What is the difference between an INNER JOIN query and an implicit join query (listing multiple tables after the FROM keyword)? For example:
INNER JOIN 查询和隐式连接查询(在 FROM 关键字后列出多个表)之间有什么区别?例如:
Given the following two tables:
鉴于以下两个表:
CREATE TABLE Statuses(
id INT PRIMARY KEY,
description VARCHAR(50)
);
INSERT INTO Statuses VALUES (1, 'status');
CREATE TABLE Documents(
id INT PRIMARY KEY,
statusId INT REFERENCES Statuses(id)
);
INSERT INTO Documents VALUES (9, 1);
What is the difference between these two SQL queries? From the testing I've done, they return the same result. Do they do the same thing? Are there situations where they will return different result sets?
这两个 SQL 查询有什么区别?根据我所做的测试,它们返回相同的结果。他们做同样的事情吗?是否存在返回不同结果集的情况?
SELECT s.description FROM Documents d, Statuses s WHERE d.statusId = s.id AND d.id = 9;
SELECT s.description FROM Documents d INNER JOIN Statuses s ON d.statusId = s.id WHERE d.id = 9;
采纳答案by HLGEM
There is no reason to ever use an implicit join (the one with the commas). Yes for inner joins it will return the same results. However, it is subject to inadvertent cross joins especially in complex queries and it is harder for maintenance because the left/right outer join syntax (deprecated in SQL Server, where it doesn't work correctly right now anyway) differs from vendor to vendor. Since you shouldn't mix implicit and explict joins in the same query (you can get wrong results), needing to change something to a left join means rewriting the entire query.
没有理由使用隐式连接(带逗号的连接)。是的,对于内部联接,它将返回相同的结果。但是,它会受到无意的交叉联接的影响,尤其是在复杂查询中,并且由于左/右外联接语法(在 SQL Server 中已弃用,无论如何现在都无法正常工作)因供应商而异,因此维护起来更加困难。由于您不应该在同一个查询中混合隐式和显式连接(您可能会得到错误的结果),因此需要将某些内容更改为左连接意味着重写整个查询。
回答by Jordan
If you do it the first way, people under the age of 30 will probably chuckle at you, but as long as you're doing an inner join, they produce the same result and the optimizer will generate the same execution plan (at least as far as I've ever been able to tell).
如果你用第一种方式,30 岁以下的人可能会嘲笑你,但只要你在做内连接,他们就会产生相同的结果,优化器会生成相同的执行计划(至少像据我所知)。
This does of course presume that the where clause in the first query is how you would be joining in the second query.
这当然假定第一个查询中的 where 子句是您加入第二个查询的方式。
This will probably get closed as a duplicate, btw.
这可能会作为重复关闭,顺便说一句。
回答by danwyand
The nice part of the second method is that it helps separates the join condition (on ...) from the filter condition (where ...). This can help make the intent of the query more readable.
第二种方法的优点在于它有助于将连接条件 (on ...) 与过滤条件 (where ...) 分开。这有助于使查询的意图更具可读性。
The join condition will typically be more descriptive of the structure of the database and the relation between the tables. e.g., the salary table is related to the employee table by the EmployeeID column, and queries involving those two tables will probably always join on that column.
连接条件通常更能描述数据库的结构和表之间的关系。例如,salary 表通过EmployeeID 列与employee 表相关,并且涉及这两个表的查询可能总是在该列上连接。
The filter condition is more descriptive of the specific task being performed by the query. If the query is FindRichPeople, the where clause might be "where salaries.Salary > 1000000"... thats describing the task at hand, not the database structure.
过滤条件更能描述查询正在执行的特定任务。如果查询是 FindRichPeople,则 where 子句可能是“wheresalary.Salary > 1000000”...这是描述手头的任务,而不是数据库结构。
Note that the SQL compiler doesn't see it that way... if it decides that it will be faster to cross join and then filter the results, it will cross join and filter the results. It doesn't care what is in the ON clause and whats in the WHERE clause. But, that typically wont happen if the on clause matches a foreign key or joins to a primary key or indexed column. As far as operating correctly, they are identical; as far as writing readable, maintainable code, the second way is probably a little better.
请注意,SQL 编译器并不这么认为……如果它决定交叉联接然后过滤结果会更快,它将交叉联接并过滤结果。它不关心 ON 子句中的内容和 WHERE 子句中的内容。但是,如果 on 子句匹配外键或连接到主键或索引列,通常不会发生这种情况。就操作正确而言,它们是相同的;就编写可读、可维护的代码而言,第二种方式可能要好一些。
回答by MUG4N
there is no difference as far as I know is the second one with the inner join the new way to write such statements and the first one the old method.
据我所知,没有区别的是第二个使用内部连接编写此类语句的新方法,第一个使用旧方法。
回答by Feisty Mango
The first one does a Cartesian product on all record within those two tables then filters by the where clause.
第一个对这两个表中的所有记录进行笛卡尔积,然后按 where 子句进行过滤。
The second only joins on records that meet the requirements of your ON clause.
第二个只连接满足 ON 子句要求的记录。
EDIT: As others have indicated, the optimization engine will take care of an attempt on a Cartesian product and will result in the same query more or less.
编辑:正如其他人所指出的,优化引擎将处理笛卡尔积的尝试,并且或多或少会导致相同的查询。
回答by sgokhales
A bit same. Can help you out.
有点一样。可以帮你。
回答by Dave
In the example you've given, the queries are equivalent; if you're using SQL Server, run the query and display the actual exection plan to see what the server's doing internally.
在您给出的示例中,查询是等效的;如果您使用 SQL Server,请运行查询并显示实际执行计划以查看服务器在内部执行的操作。