1

当我执行我的选择查询时,我得到如下结果:

注意:我对 EmpId 和 Absent_dates 列使用 Group_ConCat() 函数,因为这些列在我的 Select Query 中从其他表返回多个值

我的查询:

SELECT Group_Concat(tp.EMPCODE), tp.DEPARTMENT, Group_Concat(d.dte) Absentddate, COUNT(tp.EMPCODE) Totalnoofabsentdates
FROM test_prefixmaster tp
JOIN daterange30 d
LEFT JOIN test_prefixtransactions tpt ON (tp.EMPCODE = tpt.empcode) AND DATE(S_DateTime) = d.dte
WHERE tpt.empcode IS NULL
GROUP BY tp.EMPCODE;

执行我的查询后,结果如下:

 EmpId                 Absent_dates                   Total_no_of_Absent_days
    1101                  2012-12-02                            1
    1102,1102          2012-12-02,2012-12-03                    2
    1104,1104,1104     2012-12-02,2012-12-03,2012-12-04         3

但我的实际要求是显示如下结果:

EmpId        Absent_dates            Total_no_of_Absent_days
1101          2012-12-02                     1
1102          2012-12-02
1102          2012-12-03                     2
1104          2012-12-02
1104          2012-12-03
1104          2012-12-04                     3

任何人都可以在 MySQL 中使用什么功能吗?谢谢

4

2 回答 2

0

尝试这个

  SELECT tp.EMPCODE, tp.DEPARTMENT, d.dte Absentddate, COUNT(tp.EMPCODE) Totalnoofabsentdates
   FROM test_prefixmaster tp
   JOIN daterange30 d
  LEFT JOIN test_prefixtransactions tpt ON (tp.EMPCODE = tpt.empcode) AND DATE(S_DateTime) = d.dte
    WHERE tpt.empcode IS NULL
  ORDER BY tp.EMPCODE;
于 2013-01-04T18:43:21.240 回答
0

尝试这个:

SELECT tp.EMPCODE, tp.DEPARTMENT, d.dte, IFNULL(A.Totalnoofabsentdates , '') Totalnoofabsentdates
FROM test_prefixmaster tp 
JOIN daterange d
LEFT JOIN test_prefixtransactions tpt ON (tp.EMPCODE = tpt.empcode) AND DATE(S_DateTime) = d.dte
LEFT JOIN ( SELECT tp.EMPCODE, d.dte Absentddate, COUNT(tp.EMPCODE) Totalnoofabsentdates
            FROM test_prefixmaster tp
            JOIN daterange1 d
            LEFT JOIN test_prefixtransactions tpt ON (tp.EMPCODE = tpt.empcode) AND DATE(S_DateTime) = d.dte
            WHERE tpt.empcode IS NULL
            GROUP BY tp.EMPCODE) AS A ON tp.EMPCODE = A.EMPCODE AND d.dte = A.Absentddate; 
于 2013-01-05T10:29:33.740 回答