php - 对结果 sql 或使用 php 数组函数进行排序和排序

标签 php mysql

我有一个表“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/

相关文章:

php - MySQL:在九月和九月之间选择

php - 错误 200 - SyntaxError : JSON. 解析:意外字符

php - PDO 模拟关闭,多次绑定(bind)相同的值

MYSQL boolean 类型

mysql - 如何使用PowerShell脚本将.sql文件导入MySQL?

php - 使用php从sql表中提取列

php - 使用 AJAX 插入后,HTML 内容不应用 CSS

python - 合并 2 个表并汇总数据

PHP包含相对路径

javascript - 使用 html 表单将 javascript 变量值传递给 .php