比较内连接和外连接 SQL 语句
内连接和外连接有什么区别?这两种连接的确切含义是什么?
What is the difference between an inner join and outer join? What's the precise meaning of these two kinds of joins?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
查看 Jeff Atwood 的精彩文章:
SQL 连接的可视化解释
马克
Check out Jeff Atwood's excellent:
A Visual Explanation of SQL Joins
Marc
维基百科有一篇关于该主题的长文章[此处](http://en.wikipedia .org/wiki/Join_(SQL))
但基本上是:
Wikipedia has a nice long article on the topic [here](http://en.wikipedia.org/wiki/Join_(SQL))
But basically :
您使用 INNER JOIN 返回两个表中存在匹配的所有行。 IE。在结果表中,所有行和列都将具有值。
在 OUTER JOIN 中,结果表可能有空列。外连接可以是 LEFT 或 RIGHT
LEFT OUTER JOIN 返回第一个表中的所有行,即使第二个表中没有匹配项。
RIGHT OUTER JOIN 返回第二个表中的所有行,即使第一个表中没有匹配项。
You use INNER JOIN to return all rows from both tables where there is a match. ie. in the resulting table all the rows and columns will have values.
In OUTER JOIN the resulting table may have empty columns. Outer join may be either LEFT or RIGHT
LEFT OUTER JOIN returns all the rows from the first table, even if there are no matches in the second table.
RIGHT OUTER JOIN returns all the rows from the second table, even if there are no matches in the first table.
INNER JOIN
返回存在于两个表OUTER JOIN
返回所有行存在于任一表中的INNER JOIN
returns rows that exist in both tablesOUTER JOIN
returns all rows that exist in either table如果记录同时出现在两个表中,则内连接仅返回连接行。
根据方向的外连接将显示一个表中的所有记录,这些记录连接到存在相应行的连接表中的数据
Inner join only returns a joined row if the record appears in both table.
Outer join depending on direction will show all records from one table, joined to the data from them joined table where a corresponding row exists
使用数学 Set,
所以 (+) 是查询中的 A 方。
Using mathematical Set,
So it is (+) is your A side in the query.
假设一个包含客户和订单的示例架构:
INNER JOIN: 仅检索具有订单的客户。
LEFT OUTER JOIN:检索所有有或没有订单的客户。
RIGHT OUTER JOIN:检索具有或不具有匹配客户记录的所有订单。
有关更详细的信息,请参阅 内部和外部联接SQL语句
Assume an example schema with customers and order:
INNER JOIN: Retrieves customers with orders only.
LEFT OUTER JOIN: Retrieves all customers with or without orders.
RIGHT OUTER JOIN: Retrieves all orders with or without matching customer records.
For a slightly more detailed infos, see Inner and Outer Join SQL Statements