-9

可能重复:
如何在 mysql 表中选择、平均和排序

我在 mySql 中有一个表,就像这张图片一样

在此处输入图像描述

我想编写一个查询,其结果将按 LESSON 列分组,并添加新行,即 LESSON 列的平均值和 CNT 列值的总和....
在此处输入图像描述

我使用这个查询,但它给出的结果如图 3 所示,在这种情况下我无法按 PERC 排序

select no, STUD_ID,CLASS,LESSON, AVG(PERC) as PERC,SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by CLASS
union all
select no,STUD_ID,CLASS,'AVERAGE' as LESSON, AVG(PERC) as PERC, SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by LESSON

在此处输入图像描述

4

1 回答 1

0
select * from <your query> order by PERC

至:

select * from (

select no, STUD_ID,CLASS,LESSON, AVG(PERC) as PERC,SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by CLASS
union all
select no,STUD_ID,CLASS,'AVERAGE' as LESSON, AVG(PERC) as PERC, SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by LESSON

) as sub order by PERC DESC
于 2012-04-12T13:23:00.040 回答