mysql-select查询操作
来源:互联网 发布:矩阵分解 翻译 编辑:程序博客网 时间:2024/05/19 17:27
drop database if exists jxgl;create database jxgl;use jxgl;drop table if exists student;create table student (sno char(20) primary key, sname varchar(20), sage int(20), ssex char(20), sdept char(20));insert into student value ('2005001','钱横',18,'男','Cs');insert into student value ('2005002','王林',19,'女','Cs');insert into student value ('2005003','李民',20,'男','Is');insert into student value ('2005004','赵欣然',16,'女','Ma');insert into student value ('2005005','陈家国',20,'男','Ca');insert into student value ('2005006','陈以为',21,'男','Ma');insert into student value ('2005007','陈丙',22,'女','Cs');insert into student value ('2005008','陈丁',22,'男','Da');insert into student value ('2005009','陈式',18,'女','Ma');insert into student value ('2005010','陈看',19,'女','Df');insert into student value ('2005020','陈助',25,'女','Me');drop table if exists course;create table course (cno int(10),cname varchar(20),cpno int(10),ccredit int(10),primary key(cno));insert into course value (1,'数据库系统 ',5,4);insert into course (cno,cname,ccredit) value (2,'数学分析',2);insert into course value (3,'信息系统导论',1,3);insert into course value (4,'操作系统原理',6,3);insert into course value (5,'数据结构',7,4);insert into course value (6,'数据处理基础',Null,4);insert into course value (7,'C语言',6,3);insert into course value (8,'大学语文',1,2);insert into course value (9,'高等数学',2,2);insert into course value (10,'程序设计',3,3);insert into course value (11,'信号系统',4,4);insert into course value (12,'通信原理',5,4);insert into course value (13,'计算机组成原理',6,2);insert into course value (14,'手机开发',7,1);drop table if exists sc;create table sc(sno char(20),cno int(10),grade float(20),primary key(sno,cno), foreign key(sno) references student(sno), foreign key(cno) references course(cno));insert into sc value ('2005001',1,87);insert into sc value ('2005001',2,67);insert into sc value ('2005001',3,90);insert into sc value ('2005002',2,70);insert into sc value ('2005003',3,88);insert into sc value ('2005004',4,70);insert into sc value ('2005005',1,60);insert into sc value ('2005006',3,70);insert into sc value ('2005007',4,90);insert into sc value ('2005007',5,80);insert into sc value ('2005008',5,80);insert into sc value ('2005009',5,80);insert into sc value ('2005009',14,80);insert into sc value ('2005010',5,80);insert into sc value ('2005020',14,80);insert into sc value ('2005020',8,80);insert into sc value ('2005020',9,80);insert into sc value ('2005020',5,80);insert into sc value ('2005020',7,80);insert into sc value ('2005020',12,80);
student数据表: course数据表:
sc数据表:
(1)、统计有学生选修的课程门数;
selectcount(distinct cno) as course_sum from sc;
(2)、求选修四号课程的学生的平均年龄;
selectavg(sage) as avg_name from student, sc where student.sno = sc.sno and sc.cno ='4';
(3)、求学分为3的每门课程的学生平均成绩
selectavg(sc.grade) as avg_grade from (select cno from course where ccredit = '3') ass join sc on s.cno = sc.cno group by sc.cno;
(4)、统计每门课程的学生选修人数,要求超过3人的课程才统计,查询结果按人数的降序排列,如果相同就按课程号升序排列。
select cno,count(distinct sno) from sc group by cno having count(distinct sno) > 3 orderby count(distinct sno) desc, cno asc;
(5)、检索学号比王林大年龄比他小的学生姓名。
selects1.sname from student as s1,student as s2 where s1.sno > s2.sno and s1.sage< s2.sage and s2.sname = '王林';
(6)、检索姓名以王字开头的所有学生的姓名和年龄。
select sname,sage from student where sname like '王%';
(7)、在sc表中检索成绩为空值的学生的学号和课程号;
select sno,sc.cno from sc where grade is null;
(8)、求年龄大于女生平均年龄的男生的姓名和年龄
select sname,sage from student where ssex = '男' and sage > (select avg(sage) from student where ssex = '女');
(9)、求年龄大于所有女生年龄的男生的姓名和年龄;
select sname,sage from student where ssex = '男' and sage > all(select sage from student where ssex = '女');
(10)、检索选修四门以上课程的学生总成绩(不统计不及格课程),并要求按总成绩的降序排列;
selectsum(grade) from sc group by sno having count(cno) > 4 order by sum(grade)desc;
- mysql-select查询操作
- Mysql中的select查询操作
- mysql数据库的查询(select)操作
- (三)mysql——SQL基本操作之SELECT查询
- MySQL--操作数据表3:记录查询(SELECT)【LIMIT】
- thinkphp查询select操作
- Mysql select ..Limit操作
- sae mysql select 操作
- SQL语言-select查询操作
- hibernate mysql select *查询bug
- mysql的查询select 语句
- mysql查询语句select-having
- Mysql之查询基础select
- mysql查询语句speed select
- mysql select 语句查询排名
- Mysql select查询执行过程
- mysql 查询大全 select 大全
- MySQL--查询表达式理解--select
- ORA-00214 控制文件版本不一致恢复过程
- 处理图片的工具类
- jQuery及AngularJs的ajax请求对比
- Linux下的C语言基础编程——统计字符
- 学习java的路线图-五个必经阶段
- mysql-select查询操作
- bower安装报错”Cannot be run with sudo”解决办法
- 前端编码规范
- 递归思想解决汉诺塔的问题
- 二叉树的深度优先遍历和广度优先遍历
- Qt控件父对象的确认
- bower权限问题
- Android—Activity的四种启动模式及区别
- 用OpenCV进行视频截取