2

我有 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
4

2 回答 2

3

您应该在CASEStatement 中给出条件,而不是WHERE像这样的子句:

SELECT a.Name, b.Department,
  CASE WHEN c.Start_date <= '2012-10-05' AND c.End_date >= '2012-10-05' 
       THEN c.Start_date ELSE '' END AS Start_date
 ,CASE WHEN c.Start_date <= '2012-10-05' AND c.End_date >= '2012-10-05'
       THEN c.End_date ELSE '' END AS END_date
  FROM Personnel a
  JOIN Department b ON a.Id_personnel = b.Id_personnel
  JOIN Medical c ON a.Id_personnel = c.Id_personnel

看到这个 SQLFiddle

更新(来自评论)

如果您不想为同一部门重复同一个人,则需要将他们分组如下:

GROUP BY a.Id_personnel,Department

看到这个 SQLFiddle

更新 2

GROUP_CONCAT当同一个人有多个假期时,您需要使用函数:

SELECT DISTINCT a.Name, b.Department,
  GROUP_CONCAT(CASE WHEN c.Start_date <= '2012-10-05' 
                     AND c.End_date >= '2012-10-05' 
               THEN c.Start_date ELSE '' END SEPARATOR ' ') AS Start_date
 ,GROUP_CONCAT(CASE WHEN c.Start_date <= '2012-10-05' 
                     AND c.End_date >= '2012-10-05'
               THEN c.End_date ELSE '' END SEPARATOR '') AS END_date
  FROM Personnel a
  JOIN Department b ON a.Id_personnel = b.Id_personnel
  JOIN Medical c ON a.Id_personnel = c.Id_personnel
 GROUP BY a.Id_personnel,Department

看到这个 SQLFiddle

于 2012-11-05T13:14:00.830 回答
0

尝试这个:

 SELECT a.Name, b.Department, c.Start_date, c.End_date
      FROM Personnel a
      Left JOIN Department b ON a.Id_personnel = b.Id_personnel
      Left JOIN Medical c ON a.Id_personnel = c.Id_personnel
   WHERE c.Start_date <= 2012-10-05  AND c.End_date >= 2012-10-05
于 2012-11-05T13:01:57.403 回答