表stuinfo
id | subject | score |
1 | 语文 | 78 |
1 | 数学 | 60 |
1 | 英语 | 90 |
2 | 语文 | 50 |
2 | 数学 | 44 |
2 | 英语 | 55 |
3 | 语文 | 45 |
3 | 数学 | 44 |
3 | 英语 | 90 |
现在要求 如下 查出每个编号的总成绩 并且要大于200分
id | score→(总成绩要大于200分) |
1 | 228 |
请写出sql语句?
[code="sql"]select id, s score from (
select id,sum(score) s
from stuinfo
group by id
) t
where t.s > 200[/code]
mysql> select id,sum(score) s from table group by id having s>200;
做一次子查询即可。
1楼的方法是最好的吧,只是语法上有点小问题而已,其它的是这一方法的变种,不好
如是确定是每一编号都有3个,不求和,求平均也是一样的,反正意思一样,
select id,sum(score) s from table group by id having sum(score) >200;
select id,sum(score) s from table group by id where s>200;