简体   繁体   English

左联接-具有相同数据的两个表

[英]Left join - two tables with the same data

Lets say that that I have two simple tables with the following columns and data: 可以说我有两个带有以下列和数据的简单表:

Table 1               Table 2

year   month          year month
2017   01             2017  01
2016   12             2016  12

The primary key is a composite key that consists of the year and the month. 主键是由年和月组成的组合键。

So a classical left join, gives me all the data in the left table with the matching rows in the right table. 因此,经典的左联接为我提供了左表中的所有数据以及右表中的匹配行。

If I do a left join like this: 如果我像这样左连接:

select 
t1.year, t2.month

from 
table1 t1
   left join table 2 t2 on (t1.year = t2.year and t1.month = t2.month)

Why do I get only two rows?? 为什么我只有两行? Shouldn't I get 4 rows?? 我不应该得到4行吗?

Tnx, Tom Tnx,汤姆

A classical left join will give you the number of rows in the "Left Table" (the one in from) multiplied by the number of matches in the "Right Table" (the one in LEFT JOIN in this case), plus all the rows in the LEFT Table that have no match in the first table. 传统的左联接将为您提供“左表”中的行数(从中输入)乘以“右表”中的匹配数(在这种情况下为LEFT JOIN中的行),再加上所有行LEFT表中的第一个表中没有匹配项。

  • Number of rows in LEFT Table = 2 LEFT表中的行数= 2
  • Number of matches in Right Table = 1 右表中的匹配数= 1
  • Number of rows in LEFT Table withouth matches = 0 没有h匹配的LEFT表中的行数= 0

2 x 1 + 0 = 2 2 x 1 + 0 = 2

Edit: Actually the multiplication is given for each row. 编辑:实际上,乘法是为每一行给出的。 Would be something like 会像

Sum (row_i x matches_i) + unmatched 总和(row_i xmatches_i)+不匹配

Where row_i is means each row, and matches_i to the matches for the i row in the first table. 其中row_i表示每一行,而matchs_i表示第一个表中第i行的匹配项。 The difference with this is that each row could have different number of matches (the previous formula is only adapted to your case) 与此不同的是,每行可以有不同数量的匹配项(以前的公式仅适用于您的情况)

This will result in 这将导致

1 (row1) x 1 (matches for row 1) + 1 (row2) x 1 (matches for row 2) + 0 (unmatched rows in table 1) = result 1(行1)x 1(与第1行匹配)+ 1(行2)x 1(与第2行匹配)+ 0(表1中不匹配的行)=结果

1x1 + 1x1 + 0 = result 1x1 + 1x1 + 0 =结果

1 + 1 = 2 = result 1 + 1 = 2 =结果

If you expected 4 rows maybe you wanted to get a Cartesian Product. 如果您预期有4行,那么您可能想要获得笛卡尔积。 As the comment stated, you can use Cross Join in that case 如评论所述,在这种情况下,您可以使用交叉联接

When you join tables together, you're essentially asking the database to combine data from two different tables and display it as a single record. 将表连接在一起时,实际上是在要求数据库合并来自两个不同表的数据并将其显示为单个记录。 When you perform a left join , you are saying: 当执行left join ,您会说:

Give me all the rows from Table1, as well as any associated data from Table2 (if it exists). 给我表1中的所有行,以及表2中的任何关联数据(如果存在)。

In this sense, the data from Table2 doesn't represent separate or additional records to Table1 (even though they are stored as separate records in a separate table), it represents associated data. 在这种意义上,表2中的数据并不代表表1的单独附加记录(即使它们作为单独的记录存储在单独的表中),也代表关联的数据。 You are linking the data between the tables, not appending rows from each table. 您正在链接表之间的数据,而不是在每个表中追加行。

Imagine that Table1 stored people, and Table2 stored phone numbers. 想象一下,表1存储了人员,表2存储了电话号码。

         Table1                           Table2
+------+-------+--------+         +------+-------+-------------+
| Year | Month | Person |         | Year | Month | Phone       |
+------+-------+--------+         +------+-------+-------------+
| 2017 |    12 | Bob    |         | 2017 |    12 | 555-123-4567|
| 2016 |    01 | Frank  |         | 2016 |    01 | 555-234-5678|
+------+-------+-------+         +------+-------+--------------+

You could join them together to get a list of people and their corresponding phone numbers. 您可以将他们加入一起以获得人员列表及其相应的电话号码。 But you wouldn't expect to get a combination of rows from each table (two rows of people and two rows of phone numbers). 但是您不会期望从每个表中得到行的组合(两人和两行电话)。

You will get two rows as both the columns have 2 rows that match exactly the sam and its a composite key. 您将获得两行,因为两列都具有与sam及其组合键完全匹配的2行。 It will make the same way if you had 4 rows in each you will only get 4 rows in total. 如果每行中有4行,将以相同的方式进行操作,总共只有4行。

The Left Join takes Table1 (t1) as the Left table. 左联接将Table1(t1)作为左表。 It searches for and retrieves all values from the Right ie:- from Table 2 (t2) matching the criteria T1.Year&Month = T2.Year&Month (alias GOD/s) as well as the additional join condition T1.Month=T2.Month. 它从Right中搜索并检索所有值,即:-从表2(t2)中匹配条件T1.Year&Month = T2.Year&Month(别名GOD / s)以及附加联接条件T1.Month = T2.Month。 The result is that only 2 rows from T1 match the join criteria as well as the additional join criteria 结果是,T1中只有2行与联接条件以及其他联接条件匹配

Another takeaway : The AND T1.Month=T2.Month condition on the left join is redundant as the composite GOD key takes care of it explicitly. 另一个要点:左联接上的AND T1.Month = T2.Month条件是多余的,因为复合GOD密钥明确地处理了它。

左联接的结果

cross join returns every row you can make by combining a row from each argument. cross join通过组合每个参数的一行来返回您可以进行的每一行。 ( inner ) join on returns the rows from cross join that satisfy its condition. innerjoin oncross join返回满足其条件的行。 Ie ( inner ) join on returns every row you can make that combines a row from each argument and that satisfies its condition. 即( innerjoin on返回您可以创建的每一行,该行合并了每个参数的一行并满足其条件。

left join on returns the rows from ( inner ) join on plus the rows you can make by extending unjoined left argument rows by null for columns of the right argument. left join on收益从(行innerjoin on再加上你可以通过延长未连接的左参数行作出的行null的右参数列。

Notice that this is regardless of primary keys, unique column sets, foreign keys or any other constraints . 请注意,这与主键,唯一列集,外键或任何其他约束无关

Here there are 2 rows in each argument so there are 2 X 2 = 4 rows in the cross join . 这里每个参数有2行,因此cross join联接中有2 X 2 = 4行。 But only 2 meet the condition--the ones where a row is combined with itself. 但是只有2个满足条件-行与自身结合在一起的条件。

(If you left join a table with itself where the condition is the conjunction of one or more equalities of the left and right versions of a column and there are no null s in those columns then every left argument row gets joined with at least itself from the right argument. So there are no unjoined left argument rows. So only the rows of the ( inner ) join on are returned.) (如果您将表与自身left join在一起,而条件是一列的左右版本的一个或多个相等性的合计,并且这些列中没有null ,则每个左参数行至少与自身相连接右参数。因此,没有不连接的左参数行。因此,仅返回( innerjoin on的行。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM