-1
CREATE TABLE tab1 (
  id1 int NOT NULL,
  field11 int NOT NULL,
  field12 date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO tab1 (id1, field11, field12) VALUES
(1, 11, '2024-07-10'),
(2, 11, '2024-07-10'),
(3, 11, '2024-07-10'),
(4, 12, '2024-07-11'),
(5, 12, '2024-07-11'),
(6, 12, '2024-07-11');

CREATE TABLE tab2 (
  id2 int NOT NULL,
  field21 int NOT NULL,
  field22 date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO tab2 (id2, field21, field22) VALUES
(1, 11, '2024-07-01'),
(2, 11, '2024-07-01'),
(3, 11, '2024-07-01'),
(4, 22, '2024-07-02'),
(5, 22, '2024-07-02'),
(6, 22, '2024-07-02');
select * 
from tab1 t1
left join tab2 t2 on t1.field11 = t2.field21;

I got the following result:

+-----+---------+------------+------+---------+------------+
| id1 | field11 | field12    | id2  | field21 | field22    |
+-----+---------+------------+------+---------+------------+
|   1 |      11 | 2024-07-10 |    3 |      11 | 2024-07-01 |
|   1 |      11 | 2024-07-10 |    2 |      11 | 2024-07-01 |
|   1 |      11 | 2024-07-10 |    1 |      11 | 2024-07-01 |
|   2 |      11 | 2024-07-10 |    3 |      11 | 2024-07-01 |
|   2 |      11 | 2024-07-10 |    2 |      11 | 2024-07-01 |
|   2 |      11 | 2024-07-10 |    1 |      11 | 2024-07-01 |
|   3 |      11 | 2024-07-10 |    3 |      11 | 2024-07-01 |
|   3 |      11 | 2024-07-10 |    2 |      11 | 2024-07-01 |
|   3 |      11 | 2024-07-10 |    1 |      11 | 2024-07-01 |
|   4 |      12 | 2024-07-11 | NULL |    NULL | NULL       |
|   5 |      12 | 2024-07-11 | NULL |    NULL | NULL       |
|   6 |      12 | 2024-07-11 | NULL |    NULL | NULL       |
+-----+---------+------------+------+---------+------------+
12 rows in set (0.00 sec)

But I expect following result:

+-----+---------+------------+------+---------+------------+
| id1 | field11 | field12    | id2  | field21 | field22    |
+-----+---------+------------+------+---------+------------+
|   1 |      11 | 2024-07-10 |    1 |      11 | 2024-07-01 |
|   2 |      11 | 2024-07-10 |    2 |      11 | 2024-07-01 |
|   3 |      11 | 2024-07-10 |    3 |      11 | 2024-07-01 |
|   4 |      12 | 2024-07-11 | NULL |    NULL | NULL       |
|   5 |      12 | 2024-07-11 | NULL |    NULL | NULL       |
|   6 |      12 | 2024-07-11 | NULL |    NULL | NULL       |
+-----+---------+------------+------+---------+------------+

My expectations are based on this documentation (from https://www.w3schools.com/mysql/mysql_join_left.asp):

The LEFT JOIN keyword returns all records from the left table (table1), and the matching records (if any) from the right table (table2).

enter image description here

The results dataset must be only records that correspond to ONLY the green circle on the picture.

How does this left join work?

10
  • for that you need a row_number so that the rows are uniue so you only make a cross join , what you see in the result Commented Jul 11, 2024 at 14:27
  • 2
    You requested all records where t1.field11 = t2.field21, and that is what was returned. If you look at the id fields of each returned entry you can see that the matching pairs are all unique. Commented Jul 11, 2024 at 14:28
  • on t1.field11 = t2.field21 - For each record in tab1 with a field11 value of 11 there exist three records in tab2 with a matching field21 value. What you're seeing is the cartesian product of that join. Three records on the left, each with three matching records on the right, equals nine records. Plus the remaining three records on the left with no matching records on the right. Your expectation was simply wrong. Commented Jul 11, 2024 at 14:29
  • field11 has a row with value 11. field21 has three rows with value 11. The one row of field11 gets joined to all three because your database has no idea which one of the three rows in field21 YOU would like to join. You'll have to be more specific in your join criteria to get to your output. It's VERY important to understand that a table has no inherent ordering. The order you see as a human means nothing. Perhaps you SHOULD be joining id1 to id2 if you want that ouput. Commented Jul 11, 2024 at 14:57
  • 1
    on t1.field11 = t2.field21 AND t1.id1 = t2.id2 ? Commented Jul 11, 2024 at 16:33

1 Answer 1

-1

Yes you are right the LEFT JOIN keyword returns all records from the left table, and the matching records from the right table. Like as you performed left join on 't1.field11 = t2.field21'- All rows from the left table (t1) will be returned and rows in tab1 with field11 = '11' will join with all rows in tab2 where field21 = '11'. Since there are 3 matching rows in tab2 for field21 = 11, each row in tab1 with field11 = 11 will match with each of these 3 rows, leading to multiple combinations. Rest all will be NULL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.