sql中group by 和count 以及内连接查询的运用
来源:互联网 发布:建筑网络计划图 编辑:程序博客网 时间:2024/05/23 11:06
引用
问题描述:
现有3张表:分别是
学生信息表S(sno,sname)
课程信息表C(cno,cname,cteacher)
学生与课程关系表SC(sno,cno,scroce)
问题1:查询出所有未选修过李明老师所教课程的学生姓名.
问题2:查询出有2门以上未及格的学生姓名
问题3:查询出既选修课程"1"也选修了课程"2"的学生姓名
主要考察:group by 和inner join ,having条件筛选,以及count函数的使用
group by 主要是对结果集进行分组,然后采用having搭配使用进行数据的筛选,
- drop table S,SC,C
- create table S(
- sno varchar(15), --学号
- sname varchar(15) --学生姓名
- )
- create table C(
- cno varchar(15), --课程编号
- cname varchar(15), --课程名称
- cteacher varchar(15) --授课教师
- )
- create table SC(
- sno varchar(15), --学号
- cno varchar(15), --课程编号
- scroce float --分数
- )
- select * from S;
- delete from S;
- insert into S(sno,sname)values('s001','jerry');
- insert into S(sno,sname)values('s002','tom');
- insert into S(sno,sname)values('s003','jason');
- delete from C;
- select * from C;
- insert into C(cno,cname,cteacher)values('c001','1','李明');
- insert into C(cno,cname,cteacher)values('c002','2','李明0');
- insert into C(cno,cname,cteacher)values('c003','3','李明1');
- insert into C(cno,cname,cteacher)values('c004','4','李明2');
- delete from SC;
- select * from SC;
- insert into SC(sno,cno,scroce)values('s001','c001',40);
- insert into SC(sno,cno,scroce)values('s001','c002',50);
- insert into SC(sno,cno,scroce)values('s002','c001',50);
- insert into SC(sno,cno,scroce)values('s002','c003',50);
- select
- a.sno,a.sname,b.cno
- from S as a
- inner join SC as b on b.sno=a.sno
- where not exists(
- select * from SC where sno=b.sno and cno in(select cno from C where cteacher = '李明')
- );
- --有2门未及格的学生
- select
- a.sname
- from S as a
- inner join SC as b on b.sno=a.sno
- where b.scroce<60
- group by a.sname
- having count(sname)>1;
- --既选修了课程1也选修了课程2的学生姓名
- select
- a.sname
- from S a
- inner join SC b on a.sno=b.sno
- inner join C c on c.cno=b.cno
- where b.cno in ('c001','c002')
- group by a.sname
- having count(a.sname)=2;
- sql中group by 和count 以及内连接查询的运用
- sql语句中GROUP BY 和 HAVING的使用 count()
- sql语句中GROUP BY 和 HAVING的使用 count()
- sql语句中GROUP BY 和 HAVING的使用 count()
- sql语句中GROUP BY 和 HAVING的使用 count()
- sql语句中GROUP BY 和 HAVING的使用 count()
- sql语句中GROUP BY 和 HAVING的使用 count()
- sql语句中having count 和group by的使用
- sql语句中GROUP BY 和 HAVING的使用 count()
- sql语句中GROUP BY 和 HAVING的使用 count()
- sql语句group by以及count()的一些思考
- sql查询 -count,group by,havi..
- mysql group by内排序的实现以及having和where 条件语句查询的区别
- sql语句中,group by和having count()的使用
- SQL group by & count
- 查询所有订单的最新信息的SQL语句的写法 DISTINCT配合count和group by一起完成
- SQL中group by分组查询的内容(包含to_char和order by)
- group by 和count
- 程序猿,你关心过自己的健康吗?
- 字节对齐
- base-kernel-内存映射
- Android的第一个月
- 使用AJAX检测用户名是否可用,高手一定要绕道
- sql中group by 和count 以及内连接查询的运用
- autogrow failed
- 每天要做的事情,写下来备忘。
- 在app.config中获取ConnectionStrings配置
- MAXScript蒙皮修改器(skin)用法
- 不错的csdn博客
- 易轩之序
- rman恢复数据文件
- 从“Google 隐私权政策和服务条款发生变更” 看Google 的互联网战略的平台化更加成熟