一些简单的数据库查询功能

MySql 码拜 8年前 (2016-02-08) 882次浏览
本人最近在学习sql语言,在MySql数据库中建了如下几个表:
1.teacher:包括teacher_id和name字段
2.student:包括student_id和name字段
3.course:包括course_id和name字段
4.class:包括class_id和name字段
5.score:包括month,course,student_id,score字段
又建立了学生-班级表,老师-班级表,老师-课程表,都是多对一的,字段都是他们的id
现在想实现这几个查询并打印出来,本人的查询语句该怎么写?
1.每个老师对应有多少学生
2.每个班有多少学生
3.全部不及格的人的id,course,class,month,scorce
4.每个课程的成绩分布-即100-90-80-60各有多少人(按班级划分)
本人用的是phpmyadmin,用jdbc已经连接,设计多表查询,求大家帮帮忙啦
解决方案

45

平时上课不认真听讲,临近期末了才来发帖?

1、select b.teacher_id,count(0) from 学生-班级表 a,老师-班级表 b where a.class_id = b.class_id group by b.teacher_id;
2、select class_id,count(0) from 学生-班级表 group by class_id;
3、select * from score where score < 60;
4、select b.class_id,sum(case when a.score >= 100 and a.score < 90 then 1 else 0 end)score100_90,
sum(case when a.score >= 90 and a.score < 80 then 1 else 0 end)score90_80,
sum(case when a.score >= 80 and a.score < 60 then 1 else 0 end)score80_60
from score a,学生-班级表 b where a.student_id = b.student_id group by b.class_id

5

引用 3 楼 chaser401 的回复:
Quote: 引用 2 楼 zhangbin1988 的回复:

平时上课不认真听讲,临近期末了才来发帖?

1、select b.teacher_id,count(0) from 学生-班级表 a,老师-班级表 b where a.class_id = b.class_id group by b.teacher_id;
2、select class_id,count(0) from 学生-班级表 group by class_id;
3、select * from score where score < 60;
4、select b.class_id,sum(case when a.score >= 100 and a.score < 90 then 1 else 0 end)score100_90,
sum(case when a.score >= 90 and a.score < 80 then 1 else 0 end)score90_80,
sum(case when a.score >= 80 and a.score < 60 then 1 else 0 end)score80_60
from score a,学生-班级表 b where a.student_id = b.student_id group by b.class_id

额,兄弟,本人要是在学校就问同学了呀,为什么是count(0)呢,这里的a,b都是别名吗?

count(0),无所谓的,可以写1
a,b都是别名,是的


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明一些简单的数据库查询功能
喜欢 (0)
[1034331897@qq.com]
分享 (0)