mysql中的GROUP_CONCAT + SUM / COUNT不起作用

时间:2012-05-21 13:12:25

标签: mysql

我有一些桌子谎言这个

User         : id , username
answers      : id , answers , is_correct , question_id
user_answers : user_id , question_id , answer_id , date
questions    : id , question , category
category     : id name

现在我要做的是计算用户对每个类别给出的正确答案。我的查询是

SELECT
  u.`user_name` AS `UserName`,
  u.`user_id`   AS `UserId`,
  sum(a.is_correct) AS `Score`,
  group_concat(distinct c.name) AS `Quizes`
FROM users_answers ua
  LEFT JOIN answers a
    on a.id = ua.answer_id
  LEFT JOIN users u
    ON u.user_id = ua.user_id
  left join questions q
    on q.id = a.question_id
  left join categories c
    on c.id = q.cat_id
WHERE a.is_correct = 1
GROUP BY u.user_id
ORDER BY `Score` desc, ua.date desc;

它带来的结果还可以,但我需要为每个类别分别得到总分。

EDITS

select
  s.UserName,
  s.UserId,
  GROUP_CONCAT(Score) AS CategoryScores,
  sum(s.TotalScore) as TotalScore,
  s.Quizes,
  s.LastPlayed
from (select
        u.`user_name` AS `UserName`,
        u.`user_id`   AS `UserId`,
        sum(a.is_correct) AS `Score`,
        sum(a.is_correct) AS `TotalScore`,
        group_concat(distinct c.name) AS `Quizes`,
        ua.date       as `LastPlayed`,
        c.id          as CategoryId
      FROM users_answers ua
        LEFT JOIN answers a
          on a.id = ua.answer_id
        LEFT JOIN users u
          ON u.user_id = ua.user_id
        left join questions q
          on q.id = a.question_id
        left join categories c
          on c.id = q.cat_id
      WHERE a.is_correct = 1
      GROUP BY u.user_id, CategoryId) s
GROUP BY UserId
ORDER BY `TotalScore` desc, LastPlayed desc;

感谢迈克尔,但问题在于现在我无法在群组中获取类别名称。

2 个答案:

答案 0 :(得分:2)

试试这个

select
  UserName   as UserName,
  UserId,
  group_concat(Score) as Score,
  sum(TotalScore) as TotalScore,
  LastPlayed,
  group_concat(Quizes)    QuizNames
from (SELECT
        u.`user_name` AS `UserName`,
        u.`user_id`   AS `UserId`,
        count(ua.`id`) AS `Score`,
        count(ua.`id`) AS `TotalScore`,
        ua.date       as `LastPlayed`,
        group_concat(distinct c.name) as `Quizes`
      FROM users_answers ua
        LEFT JOIN answers a
          on a.id = ua.answer_id
        LEFT JOIN users u
          ON u.user_id = ua.user_id
        left join questions q
          on q.id = a.question_id
        left join categories c
          on c.id = q.cat_id
      WHERE a.is_correct = 1
      GROUP BY u.user_id desc,c.id
      ORDER BY `Score` desc, ua.date desc,u.user_id) sub
GROUP BY UserId desc
ORDER BY `TotalScore` desc, LastPlayed desc

答案 1 :(得分:1)

c.id添加到您的SELECT列表和GROUP BY。这将导致SUM(a.is_correct)跨类别ID进行分组。

SELECT
  u.`user_name` AS `UserName`,
  u.`user_id`   AS `UserId`,
  /* CategoryId into the SELECT list */
  c.id AS `CategoryId`,
  sum(a.is_correct) AS `Score`,
  group_concat(distinct c.name) AS `Quizes`
FROM users_answers ua
  LEFT JOIN answers a
    on a.id = ua.answer_id
  LEFT JOIN users u
    ON u.user_id = ua.user_id
  left join questions q
    on q.id = a.question_id
  left join categories c
    on c.id = q.cat_id
WHERE a.is_correct = 1
/* And add it to the GROUP BY */
GROUP BY 
  u.user_id,
  CategoryId
ORDER BY `Score` desc, ua.date desc, CategoryId;

更新

在评论中澄清问题之后,将整个事物包装在子查询中,并在总和上调用GROUP_CONCAT()

SELECT
  subq.UserName,
  subq.UserId,
  GROUP_CONCAT(CONCAT(CategoryName, ': ', Score)) AS CategoryScores,
  Quizzes
FROM (
    SELECT
      u.`user_name` AS `UserName`,
      u.`user_id`   AS `UserId`,
      /* CategoryId into the SELECT list */
      c.id AS `CategoryId`,
      c.name AS `CategoryName`,
      sum(a.is_correct) AS `Score`,
      group_concat(distinct c.name) AS `Quizes`
    FROM users_answers ua
      LEFT JOIN answers a
        on a.id = ua.answer_id
      LEFT JOIN users u
        ON u.user_id = ua.user_id
      left join questions q
        on q.id = a.question_id
      left join categories c
        on c.id = q.cat_id
    WHERE a.is_correct = 1
    /* And add it to the GROUP BY */
    GROUP BY 
      u.user_id,
      CategoryId,
      CategoryName
) subq
GROUP BY UserId
ORDER BY `Score` desc, ua.date desc;