在SQL中,Join实际上是Intersection?而且还是联动还是“横行联盟”?
我一直认为 SQL 中的 Join 是两个表之间的某种链接。
例如,
select e.name, d.name from employees e, departments d
where employees.deptID = departments.deptID
在本例中,它链接两个表,以显示每个员工的部门名称而不是部门 ID。有点像“联动”或“并集”。
但是,在了解了内连接与外连接之后,就会发现,Join(内连接)实际上是一个交集。
例如,当一个表的 ID 为 1 时, 2, 7, 8,而另一个表的ID只有7和8,我们获取交集的方法是:
select * from t1, t2 where t1.ID = t2.ID
获取“7和8”的两条记录,所以它实际上是一个交集,
所以我们有“。 2 个表的“交集”。将其与 2 个表的“并集”操作进行比较。Join 可以被视为“交集”吗?但是它的“链接”或“旁路并集”方面又如何呢?
I always thought of a Join in SQL as some kind of linkage between two tables.
For example,
select e.name, d.name from employees e, departments d
where employees.deptID = departments.deptID
In this case, it is linking two tables, to show each employee with a department name instead of a department ID. And kind of like a "linkage" or "Union" sideway".
But, after learning about inner join vs outer join, it shows that a Join (Inner join) is actually an intersection.
For example, when one table has the ID 1, 2, 7, 8, while another table has the ID 7 and 8 only, the way we get the intersection is:
select * from t1, t2 where t1.ID = t2.ID
to get the two records of "7 and 8". So it is actually an intersection.
So we have the "Intersection" of 2 tables. Compare this with the "Union" operation on 2 tables. Can a Join be thought of as an "Intersection"? But what about the "linking" or "sideway union" aspect of it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
外部联接 - 与 Union 或 Union All 无关。
例如,Union 或 Union All 操作不会出现“null”,但它是外连接的结果。
An Outer Join - is not related to - Union or Union All.
For example, a 'null' would not occur as a result of Union or Union All operation, but it results from an Outer Join.
INNER JOIN
将两个NULL
视为两个不同的值。因此,如果您基于可为空的列进行联接,并且两个表在该列中都有 NULL 值,则 INNER JOIN 将忽略这些行。因此,要正确检索两个表之间的所有公共行,应使用
INTERSECT
。INTERSECT
将两个NULL
视为相同的值。示例(SQLite):
创建两个具有可为空列的表:
插入
NULL
值:使用
INNER JOIN
检索公共行(这不显示输出):使用
INTERSECT
检索公共行(这正确显示了公共行):结论:
尽管如此,很多时候
INTERSECT
和INNER JOIN
可用于获取 结果相同,并不相同,应根据情况进行选择。INNER JOIN
treats twoNULL
s as two different values. So, if you join based on a nullable column, and if both tables haveNULL
values in that column, thenINNER JOIN
will ignore those rows.Therefore, to correctly retrieve all common rows between two tables,
INTERSECT
should be used.INTERSECT
treats twoNULL
s as the same value.Example(SQLite):
Create two tables with nullable columns:
Insert
NULL
values:Retrieve common rows using
INNER JOIN
(This shows no output):Retrieve common rows using
INTERSECT
(This correctly shows the common row):Conclusion:
Even though, many times both
INTERSECT
andINNER JOIN
can be used to get the same results, they are not the same and should be picked depending on the situation.你走在正确的道路上;
INNER JOIN
返回的行是满足连接条件的行。但这就像交集,只是因为您在连接条件中使用了等式,并将其应用于每个表中的列。另请注意,
INTERSECTION
已经是一个 SQL 操作,并且它具有其他含义 - 并且它与JOIN
不同。SQL
JOIN
可以生成一种新类型的行,其中包含两个连接表中的所有列。例如:col4、col5 和 col6 不存在于表 A 中,但它们确实存在于与表 B 联接的结果中:SQL
INTERSECTION
返回两个单独表共有的行,其中必须已经具有相同列。这恰好会产生与以下联接相同的结果:
并非每个品牌的数据库都支持 INTERSECTION 运算符。
You're on the right track; the rows returned by an
INNER JOIN
are those that satisfy the join conditions. But this is like an intersection only because you're using equality in your join condition, applied to columns from each table.Also be aware that
INTERSECTION
is already an SQL operation and it has another meaning -- and it's not the same asJOIN
.An SQL
JOIN
can produce a new type of row, which has all the columns from both joined tables. For example: col4, col5, and col6 don't exist in table A, but they do exist in the result of a join with table B:An SQL
INTERSECTION
returns rows that are common to two separate tables, which must already have the same columns.This happens to produce the same result as the following join:
Not every brand of database supports the
INTERSECTION
operator.连接“链接”或 erm... 连接两个表中的行。我认为这就是你所说的“横向联盟”的意思,尽管我个人认为这是一种糟糕的表达方式。但不同类型的连接执行的操作略有不同:
Jeff 的页面阿特伍德的博客描述了其他可能性。
A join 'links' or erm... joins the rows from two tables. I think that's what you mean by 'sideways union' although I personally think that is a terrible way to phrase it. But there are different types of joins that do slightly different things:
This page on Jeff Atwood's blog describes other possibilities.