0

嘿,我正在尝试使用从子查询返回的结果来订购 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

对此提供帮助将不胜感激。

4

2 回答 2

4

我对您的查询感到困惑,您订购的 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
于 2013-06-03T16:48:38.187 回答
0

我发现您的查询没有任何问题。试试这个 sql。它会起作用的。

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.student_id=tb2.student_id
GROUP BY tb1.stud_id , tb1.stud_name
ORDER BY sum(tb2.score) DESC
于 2013-06-03T16:48:52.840 回答