我有一个数据库,其中有一个用户表&他们有多少时间奖励记录表我想要这样的结果(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一起使用,但是对于这个期望结果没有任何替代方法
答案 0 :(得分:1)
您有2级分组,第一周,然后是用户名, 所以我建议你使用子查询进行第一次分组,如:
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;