0

私はこれに似たいくつかのテーブルを持っています

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;

結果は大丈夫ですが、カテゴリごとに個別に合計スコアを取得する必要があります。

編集

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;

Michaelに感謝しますが、ここでの問題は、グループconcatでカテゴリ名を取得できないことです。

4

2 に答える 2

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
于 2012-05-22T06:57:54.063 に答える
1

リストに追加c.idして。これにより、はカテゴリID間でグループ化されます。 SELECTGROUP BYSUM(a.is_correct)

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;
于 2012-05-21T13:15:01.267 に答える