如何在sql中按顺序使用avg

时间:2017-03-25 06:17:17

标签: mysql

嗨,我刚接触sql,遇到这个问题。 我按平均分数的升序和通过率的降序排序表子代码。 代码是

select * 
from subjcode 
group by sno 
order by(select avg(score) from subjcode group by sno)asc ;

它从来没有工作过alwats说错误代码:1242。

下表名为subjcode:

Sno     cno     score
S001    C001    78.90
S001    C002    82.90
S001    C003    59.00
S002    C001    80.90
S002    C002    72.90
S003    C001    81.90
S003    C002    81.90
S004    C001    60.90

请有人帮帮我吗?谢谢

1 个答案:

答案 0 :(得分:1)

您不需要将AVG()放入另一个SELECT。只是:

ORDER BY AVG(score)

但是,在您进行分组时使用SELECT *是没有意义的。将从组中的随机行中选择所有其他列。你应该这样做:

SELECT sno, AVG(score) AS avg
FROM subjcode
GROUP BY sno
ORDER BY avg