Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
排序结果mysql或使用php数组函数_Php_Mysql - Fatal编程技术网

排序结果mysql或使用php数组函数

排序结果mysql或使用php数组函数,php,mysql,Php,Mysql,我有一张“学生分数”表 等等 此表包含大约1000条用户和点的记录。 我想要10条基于分数的记录(最高分数优先) 所以我可以使用mysql查询作为 Select * from student_points order by points limit 0,10 现在的要求是,我们需要根据用户id对这10条记录进行分组 例如,在前10个记录中,3个是3个学生记录,所以他们应该在组中显示。 最终结果应该是这样的 id user_id points subject_id

我有一张“学生分数”表

等等

此表包含大约1000条用户和点的记录。 我想要10条基于分数的记录(最高分数优先) 所以我可以使用mysql查询作为

Select * from student_points order by points limit 0,10
现在的要求是,我们需要根据用户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
您可以看到,第一条记录是基于most点的,it学生id是12,现在它们是根据用户id分组的

我试过两次点菜。 在得到结果后,我还尝试数组_multisort,但两者都不能正常工作

请给出任何建议,无论是mysql查询还是在得到结果后分组


谢谢

我想您需要将查询包装在子查询中,然后根据
用户id
点数对结果进行排序

SELECT * FROM 
(
  Select * from student_points order by points limit 0,10
) AS t
ORDER BY 
  t.user_id DESC, 
  t.points DESC

这应该是可行的,只要给你想要限制的数字加上一个限制就行了

select sp.id, sp.user_id, sp.points 
from student_points sp
join (select user_id, max(points) as sort_by from student_points group by user_id) sort_table on sp.user_id = sort_table.user_id
order by sort_table.sort_by desc, sp.user_id, sp.points desc;

为了获得所需的结果,我编写了查询:

SELECT * FROM (
    Select * from student_points order by points limit 0,10
) As st
GROUP BY user_id,subject_id
ORDER BY points DESC

请试试这个。如果这对您不起作用,请告诉我。

您希望每个
用户id
按分数降序排序的10条记录吗?按用户id描述的顺序,点数描述?您也希望汇总分数,对吗?您想要的最终结果是什么?您好,为了更好地理解,我编辑了问题,现在有了科目id。这10个结果可以根据不同科目的分数记录3个学生的成绩,我只想根据用户id对他们进行分组。我很想听听背后的原因downvoting@1000111,您好,请重新阅读问题,我已编辑。您的要求仍然可以通过此查询实现。请尝试一下,并让我知道@rich5757这不会首先显示得分最高的学生ID/组,因此没有完全满足条件。也许我遗漏了什么。事实上我没有收到你的留言。请在这里@jeroen发布一个答案好吗
SELECT * FROM (
    Select * from student_points order by points limit 0,10
) As st
GROUP BY user_id,subject_id
ORDER BY points DESC