嘿,我正在尝试使用子查询的返回结果来订购SQL查询,即

SELECT tb1.stud_id , tb1.stud_name , (SELECT sum(score) FROM scores WHERE student_id = tb1.
student) AS total_marks
FROM Students_info AS tb1
GROUP BY tb1.stud_id , tb1.stud_name
ORDER BY total_marks DESC


我也尝试过

ORDER BY (SELECT sum(score) FROM scores WHERE student_id = tb1.student) DESC


在此方面的协助将不胜感激。

最佳答案

我对您的查询感到困惑,您订购的select语句将为每个学生返回相同的结果,因为它与students_info表无关。

我假设您想要这样的东西:

SELECT tb1.stud_id , tb1.stud_name , SUM(tb2.score) AS total_marks
FROM Students_info AS tb1
LEFT JOIN scores AS tb2
    ON tb1.stud_id = tb2.student_id
GROUP BY tb1.stud_id , tb1.stud_name
ORDER BY total_marks DESC

关于mysql - 使用子查询的返回结果对SQL查询进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16901683/

10-13 01:37