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

chaser401 2016-04-15 04:47:03
我最近在学习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已经连接,设计多表查询,求大家帮帮忙啦
...全文
117 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rick-he 2016-04-15
  • 打赏
  • 举报
回复
引用 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都是别名吗?[/quote] count(0),无所谓的,可以写1 a,b都是别名,是的
chaser401 2016-04-15
  • 打赏
  • 举报
回复
引用 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都是别名吗?
ayzen1988 2016-04-15
  • 打赏
  • 举报
回复
平时上课不认真听讲,临近期末了才来发帖?

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
chaser401 2016-04-15
  • 打赏
  • 举报
回复
我的查询方法已经写好: public static int select(String str) { Connection conn = DbUtil.getConn();// 调用类方法的方式 int i = 0; String sql = str; ResultSet rs = null; PreparedStatement pstmt = null; try { pstmt = (PreparedStatement) conn.prepareStatement(sql); rs = (ResultSet) pstmt.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); // 得到数据集的列数 while (rs.next()) { for (int j = 1; j < numberOfColumns + 1; j++) { System.out.print(rsmd.getColumnName(j) + "\t" + rs.getObject(j) + "\t" ); } } } catch (SQLException e) { e.printStackTrace(); } finally { DbUtil.closeRs(rs); DbUtil.closePs(pstmt); DbUtil.closeConn(conn); } return i; } 只要传一个sql语句进来应该就行了吧

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧