私は3つのテーブルを持っています。最初のものは人員のリストです:
テーブルPersonnel
Id_personnel Name
1 John
2 Alice
3 Tom
4 Charles
5 Ben
Table Department
Id_personnel Department
1 Department 1
2 Department 2
3 Department 3
4 Department 4
5 Department 5
and table with medical leave:
Table Medical
Id_personnel Start_date End_date
1 2012-08-02 2012-08-05
2 2012-08-02 2012-08-15
3 2012-10-04 2012-10-06
4 2012-10-04 2012-10-06
5 2012-09-20 2012-09-21
For a given date (let's say 2012-10-05) I want to have something like
Department 1 John
Department 2 Alice
Department 3 Tom 2012-10-04 2012-10-06
Department 4 Charles 2012-10-04 2012-10-06
Department 5 Ben
Using INNER JOIN I am able to get only those sick. I have something like this:
SELECT a.Name, b.Department, c.Start_date, c.End_date FROM Personnel a
INNER JOIN Department b ON a.Id_personnel = b.Id_personnel
INNER JOIN Medical c ON a.Id_personnel = c.Id_personnel
WHERE c.Start_date <= 2012-10-05 AND c.End_date >= 2012-10-05
And the obvious wrong result is:
Department 3 Tom 2012-10-04 2012-10-06
Department 4 Charles 2012-10-04 2012-10-06