嘿,我正在尝试使用子查询的返回结果来命令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
非常感谢对此的协助。
答案 0 :(得分: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
答案 1 :(得分: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