简体   繁体   中英

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??

Tnx, Tom

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.

  • Number of rows in LEFT Table = 2
  • Number of matches in Right Table = 1
  • Number of rows in LEFT Table withouth matches = 0

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

Where row_i is means each row, and matches_i to the matches for the i row in the first table. 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

1x1 + 1x1 + 0 = result

1 + 1 = 2 = result

If you expected 4 rows maybe you wanted to get a Cartesian Product. 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:

Give me all the rows from Table1, as well as any associated data from Table2 (if it exists).

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. You are linking the data between the tables, not appending rows from each table.

Imagine that Table1 stored people, and Table2 stored phone numbers.

         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. It will make the same way if you had 4 rows in each you will only get 4 rows in total.

The Left Join takes Table1 (t1) as the Left table. 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. The result is that only 2 rows from T1 match the join criteria as well as the additional join criteria

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.

左联接的结果

cross join returns every row you can make by combining a row from each argument. ( inner ) join on returns the rows from cross join that satisfy its condition. Ie ( inner ) join on returns every row you can make that combines a row from each argument and that satisfies its condition.

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.

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 . But only 2 meet the condition--the ones where a row is combined with itself.

(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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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