mysql查询两门及两门以上不及格者的平均成绩

查询两门及两门以上不及格者的平均成绩

create table stu(name char(3),subject char(3),score tinyint) CHARSET = utf8;

set names gbk;//为了插入中文

insert into stu values
('张三','数学',80),
('张三','语文',70),
('李四','数学',40),
('李四','语文',50),
('王五','语文',45),
('王五','数学',55);
+------+---------+-------+
| name | subject | score |
+------+---------+-------+
| 张三 | 数学    |    80 |
| 张三 | 语文    |    70 |
| 李四 | 数学    |    40 |
| 李四 | 语文    |    50 |
| 王五 | 语文    |    45 |
| 王五 | 数学    |    55 |
+------+---------+-------+

select name,sum(score<60) as gk,avg(score) as pj from stu group by name;
+------+------+---------+
| name | gk   | pj      |
+------+------+---------+
| 张三 | 0    | 75.0000 |
| 李四 | 2    | 45.0000 |
| 王五 | 2    | 50.0000 |
+------+------+---------+

select name,sum(score<60) as gk,avg(score) as pj from stu group by name having gk>=2;
+------+------+---------+
| name | gk   | pj      |
+------+------+---------+
| 李四 | 2    | 45.0000 |
| 王五 | 2    | 50.0000 |
+------+------+---------+



你可能感兴趣的:(mysql)