如何在MySQL中逐行显示记录字符串

时间:2013-01-04 18:32:41

标签: mysql sql select count

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

注意:我已经为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;

Afetr执行我的查询以下是结果:

 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中使用什么功能? 感谢

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;