我有一个数据库,其中有一个用户表,他们想奖励记录表多少次,我想这样得到(WEEK WISE)

user_name user_week user_reward
abc          24,25,26    4,5,6


我的查询是这样的

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


结果是

user_name user_week user_reward
abc          24    4
abc          24    5
abc          24    6


我使用group_concat与count,但它没有用,是否有任何替代方法可用于此期望结果

最佳答案

您有两个级别的分组,第一个星期,然后是用户名,
因此,我建议您对第一个分组使用子查询,例如:

SELECT
 user_name,
 GROUP_CONCAT(user_week) AS user_weeks,
 GROUP_CONCAT(user_reward) AS user_rewards
FROM (
  SELECT
   user_name,
   WEEK(cpd.added_date) AS user_week,
   COUNT(cpd.result) AS user_reward
  FROM cron_players_data AS cpd
  WHERE
   cpd.player_id = 81 AND
   cpd.result = 2 AND
   cpd.status = 1
  GROUP BY user_week
) as temp
GROUP BY user_name;

10-06 08:40