如何在mysql Query中应用GROUP_CONCAT

时间:2012-06-27 14:49:28

标签: mysql sql group-concat

如何在此查询中应用GROUP_CONCAT

查询

SELECT WEEK(cpd.added_date) AS week_no,COUNT(cpd.result) AS death_count
FROM cron_players_data cpd WHERE cpd.player_id = 81
AND cpd.result = 2 AND cpd.status = 1
GROUP BY WEEK(cpd.added_date);

查询输出结果屏幕

enter image description here

需要结果

23,24,25 AS week_no 2,3,1 AS death_count  

2 个答案:

答案 0 :(得分:3)

试试这个

select
  group_concat(t.week_no) as Weeks,
  group_concat(t.death_count) as DeathCounts
from (SELECT
    WEEK(cpd.added_date) AS week_no,
    COUNT(cpd.result) AS death_count
      FROM cron_players_data cpd
      WHERE cpd.player_id = 81
      AND cpd.result = 2
      AND cpd.status = 1
      GROUP BY WEEK(cpd.added_date)) as t

答案 1 :(得分:1)

这个替代解决方案如何:

查询

select group_concat( week_no ) as week_no, group_concat( death_count ) as death_count
from (
    SELECT WEEK(cpd.added_date) AS week_no,COUNT(cpd.result) AS death_count
    FROM cron_players_data cpd WHERE cpd.player_id = 81
    AND cpd.result = 2 AND cpd.status = 1
    GROUP BY WEEK(cpd.added_date)
) grouped_data
;