我有一个表“student_points”。
id user_id points subject_id
1 10 45 22
2 11 75 23
3 12 78 24
4 10 13 23
5 12 65 23
等等...
该表包含大约1000条用户和积分的记录。 我想要 10 个基于分数的记录(首先是最大分数) 所以我可以使用 mysql 查询作为
Select * from student_points order by points limit 0,10
现在的需求是我们需要根据user_id对这10条记录进行分组 例如,在前 10 条记录中,有 3 条是 3 名学生的记录,因此他们应该分组显示。 最终结果应该是这样的
id user_id points subject_id
3 12 78 24
5 12 65 23
1 10 45 22
4 10 13 23
2 11 75 23
可以看到第一条记录是按照最点的,student id是12,现在按照user_id分组。
我尝试了两个 order by 。 得到结果后,我也尝试了 array_multisort,但两者都无法正常工作。
请提出任何建议,mysql查询或得到结果后分组。
谢谢
最佳答案
为了获得您需要的结果,我编写了查询:
SELECT * FROM (
Select * from student_points order by points limit 0,10
) As st
GROUP BY user_id,subject_id
ORDER BY points DESC
请试试这个。如果这对您不起作用,请告诉我。
关于php - 对结果 sql 或使用 php 数组函数进行排序和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39614347/