In the following case:
CREATE TABLE Persons (
groupId int,
age int,
Person varchar(255)
);
insert into Persons (Person, groupId, age) values('Bob' , 1 , 32);
insert into Persons (Person, groupId, age) values('Jill' , 1 , 34);
insert into Persons (Person, groupId, age)values('Shawn' , 1 , 42);
insert into Persons (Person, groupId, age) values('Shawn' , 1 , 42);
insert into Persons (Person, groupId, age) values('Jake' , 2 , 29);
insert into Persons (Person, groupId, age) values('Paul' , 2 , 36);
insert into Persons (Person, groupId, age) values('Laura' , 2 , 39);
The following query:
SELECT *
FROM `Persons` o
LEFT JOIN `Persons` b
ON o.groupId = b.groupId AND o.age < b.age
returns (executed in http://sqlfiddle.com/#!9/cae8023/5):
1 32 Bob 1 34 Jill
1 32 Bob 1 42 Shawn
1 34 Jill 1 42 Shawn
1 32 Bob 1 42 Shawn
1 34 Jill 1 42 Shawn
1 42 Shawn (null) (null) (null)
1 42 Shawn (null) (null) (null)
2 29 Jake 2 36 Paul
2 29 Jake 2 39 Laura
2 36 Paul 2 39 Laura
2 39 Laura (null) (null) (null).
I don't understand the result.
I was expecting
1 32 Bob 1 34 Jill
1 32 Bob 1 42 Shawn
1 34 Jill 1 42 Shawn
1 42 Shawn (null) (null) (null)
2 29 Jake 2 36 Paul
2 29 Jake 2 39 Laura
2 39 Laura (null) (null) (null)
Reason I was expecting that is that in my understanding the left join picks each row from the left table, tries to match it each row of the right table and if there is a match it adds the row. If there is no match in the condition it adds the left row with null values for the right columns.
So if that is correct why in the fiddle output we have after
1 34 Jill 1 42 Shawn
rows for Bob and Jill repeated?