[英]How to get multiple max column value mysql
I am trying to query tables using join but I can't get success here my tables 我正在尝试使用联接查询表,但是我的表在这里无法获得成功
Score
id , user_id , score , week
1 , 123 , 2 , 1
2 , 432 , 2 , 1
3 , 432 , 3 , 1
4 , 123 , 2 , 2
I want to query top twenty users records on the bases of sum of week 1 and week 2's max total 我想根据第1周和第2周的最大总和查询前20个用户记录
I want the result should be 我希望结果应该是
id , user_id , week1score , week2score
1 , 123 , 2 , 2
2 , 432 , 3 , 0
The formula is order by descending sum(max(week1)+max(week2)) 公式按总和(max(week1)+ max(week2))降序排列
Thanks Inam 谢谢伊南
something like this would do 这样的事情会做
SELECT t1.user_id,
SUM(week1score) AS week1score,
SUM(week2score) AS week2score
FROM (SELECT user_id
FROM `table`
GROUP BY user_id) AS t1
JOIN (SELECT user_id,
Max(IF(`week` = 1, score, 0)) AS week1score,
Max(IF(`week` = 2, score, 0)) AS week2score
FROM `table`
GROUP BY user_id,
`week`) t2
ON t2.user_id = t1.user_id
GROUP BY user_id
ORDER BY ( SUM(week1score) + SUM(week2score) ) DESC
You need a composit index on the following columns 您需要在以下列上使用复合索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.