在SQL中,Join实际上是Intersection?而且还是联动还是“横行联盟”?

发布于 2024-08-29 18:46:56 字数 527 浏览 4 评论 0原文

我一直认为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

李不 2024-09-05 18:46:57

外部联接 - 与 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.

远山浅 2024-09-05 18:46:57

INNER JOIN 将两个 NULL 视为两个不同的值。因此,如果您基于可为空的列进行联接,并且两个表在该列中都有 NULL 值,则 INNER JOIN 将忽略这些行。

因此,要正确检索两个表之间的所有公共行,应使用INTERSECTINTERSECT 将两个 NULL 视为相同的值。

示例(SQLite):

创建两个具有可为空列的表:

CREATE TABLE Table1 (id INT, firstName TEXT);
CREATE TABLE Table2 (id INT, firstName TEXT);

插入NULL值:

INSERT INTO Table1 VALUES (1, NULL);
INSERT INTO Table2 VALUES (1, NULL);

使用INNER JOIN检索公共行(这不显示输出):

SELECT * FROM Table1 INNER JOIN Table2 ON 
    Table1.id=Table2.id AND Table1.firstName=Table2.firstName;

使用INTERSECT检索公共行(这正确显示了公共行):

SELECT * FROM Table1 INTERSECT SELECT * FROM Table2;

结论:

尽管如此,很多时候INTERSECTINNER JOIN 可用于获取 结果相同,并不相同,应根据情况进行选择。

INNER JOIN treats two NULLs as two different values. So, if you join based on a nullable column, and if both tables have NULL values in that column, then INNER JOIN will ignore those rows.

Therefore, to correctly retrieve all common rows between two tables, INTERSECT should be used. INTERSECT treats two NULLs as the same value.

Example(SQLite):

Create two tables with nullable columns:

CREATE TABLE Table1 (id INT, firstName TEXT);
CREATE TABLE Table2 (id INT, firstName TEXT);

Insert NULL values:

INSERT INTO Table1 VALUES (1, NULL);
INSERT INTO Table2 VALUES (1, NULL);

Retrieve common rows using INNER JOIN (This shows no output):

SELECT * FROM Table1 INNER JOIN Table2 ON 
    Table1.id=Table2.id AND Table1.firstName=Table2.firstName;

Retrieve common rows using INTERSECT (This correctly shows the common row):

SELECT * FROM Table1 INTERSECT SELECT * FROM Table2;

Conclusion:

Even though, many times both INTERSECT and INNER JOIN can be used to get the same results, they are not the same and should be picked depending on the situation.

羞稚 2024-09-05 18:46:56

你走在正确的道路上; INNER JOIN 返回的行是满足连接条件的行。但这就像交集,只是因为您在连接条件中使用了等式,并将其应用于每个表中的列。

另请注意,INTERSECTION 已经是一个 SQL 操作,并且它具有其他含义 - 并且它与 JOIN 不同。

SQL JOIN 可以生成一种新类型的行,其中包含两个连接表中的所有列。例如:col4、col5 和 col6 不存在于表 A 中,但它们确实存在于与表 B 联接的结果中:

SELECT a.col1, a.col2, a.col3, b.col4, b.col5, b.col6
FROM A INNER JOIN B ON a.col2=b.col5;

SQL INTERSECTION 返回两个单独表共有的行,其中必须已经具有相同列。

SELECT col1, col2, col3 FROM A
INTERSECT
SELECT col1, col2, col3 FROM B;

这恰好会产生与以下联接相同的结果:

SELECT a.col1, a.col2, a.col3
FROM A INNER JOIN B ON a.col1=b.col1 AND a.col2=b.col2 AND a.col3=b.col3;

并非每个品牌的数据库都支持 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 as JOIN.

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:

SELECT a.col1, a.col2, a.col3, b.col4, b.col5, b.col6
FROM A INNER JOIN B ON a.col2=b.col5;

An SQL INTERSECTION returns rows that are common to two separate tables, which must already have the same columns.

SELECT col1, col2, col3 FROM A
INTERSECT
SELECT col1, col2, col3 FROM B;

This happens to produce the same result as the following join:

SELECT a.col1, a.col2, a.col3
FROM A INNER JOIN B ON a.col1=b.col1 AND a.col2=b.col2 AND a.col3=b.col3;

Not every brand of database supports the INTERSECTION operator.

稀香 2024-09-05 18:46:56

连接“链接”或 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:

  • An inner join is indeed an intersection.
  • A full outer join is a union.

This page on Jeff Atwood's blog describes other possibilities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文