(sql server 2000)数据库group by的正确用法

来源:百度知道 编辑:UC知道 时间:2024/05/10 05:54:42
一个表分3列,分别是sno(学号) cno(课程号) score(成绩)
现在求出每门课中成绩最低的学号,课程号,成绩
我做了以下代码:
select sno,cno,min(score)
from grade
group by cno

执行查询后出现如下错误:
服务器: 消息 8120,级别 16,状态 1,行 1
列 'grade.sno' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

应该怎么办啊?

select sno,cno,score from grade,(
select cno,min(score) as score
from grade
group by cno )as table1
where table1.cno=grade.cno and table1.score=grade.score

select sno,cno,min(score)
from grade
group by cno, sno
就可以了

group by 是分组的意思,上面要查的sno 也要进行分组
bochins 的答案ok

如果想要不出问题,所有select后面出现的字段都应该在groupby里面出现