Home > Backend Development > PHP Tutorial > 请教大家一个SQL的问题.

请教大家一个SQL的问题.

WBOY
Release: 2016-06-23 14:01:35
Original
896 people have browsed it

有一个test数据库,里面有一张学员表student,有三个字段,class,name,score,分别表示班级,姓名,成绩,问:查询每个班级的及格人数和不及格人数,输出格式是class,及格人数,不及格人数

这个该怎么写呢?


回复讨论(解决方案)

select class,sum(case score >=60 then 1 else 0 end) as '及格人数',sum(case score < 60 then 1 else 0 end) as '不及格人数'from student group by class
Copy after login

select class,(select count(*) from student where class=stu.class and score >=60 ) as '及格人数',(select count(*) from student where class=stu.class and score <60 ) as '及格人数',from student stu group by class
Copy after login

哦,写错了。更正:

select class,(select count(*) from student where class=stu.class and score >=60 ) as '及格人数',(select count(*) from student where class=stu.class and score <60 ) as '不及格人数',from student stu group by class
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template