mysql应用实例
目录:
- 表结构
- sql练习
1、表结构
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
-- Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`cid` INT(11) NOT NULL AUTO_INCREMENT,
`caption` VARCHAR(32) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=INNODB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `class`
-- ----------------------------
BEGIN;
INSERT INTO `class` VALUES ('', '三年二班'), ('', '三年三班'), ('', '一年二班'), ('', '二年九班');
COMMIT; -- ----------------------------
-- Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`cid` INT(11) NOT NULL AUTO_INCREMENT,
`cname` VARCHAR(32) NOT NULL,
`teacher_id` INT(11) NOT NULL,
PRIMARY KEY (`cid`),
KEY `fk_course_teacher` (`teacher_id`),
CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
) ENGINE=INNODB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `course`
-- ----------------------------
BEGIN;
INSERT INTO `course` VALUES ('', '生物', ''), ('', '物理', ''), ('', '体育', ''), ('', '美术', '');
COMMIT; -- ----------------------------
-- Table structure for `score`
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
`sid` INT(11) NOT NULL AUTO_INCREMENT,
`student_id` INT(11) NOT NULL,
`course_id` INT(11) NOT NULL,
`num` INT(11) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_score_student` (`student_id`),
KEY `fk_score_course` (`course_id`),
CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
) ENGINE=INNODB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `score`
-- ----------------------------
BEGIN;
INSERT INTO `score` VALUES ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', ''), ('', '', '', '');
COMMIT; -- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`sid` INT(11) NOT NULL AUTO_INCREMENT,
`gender` CHAR(1) NOT NULL,
`class_id` INT(11) NOT NULL,
`sname` VARCHAR(32) NOT NULL,
PRIMARY KEY (`sid`),
KEY `fk_class` (`class_id`),
CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
) ENGINE=INNODB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `student`
-- ----------------------------
BEGIN;
INSERT INTO `student` VALUES ('', '男', '', '理解'), ('', '女', '', '钢蛋'), ('', '男', '', '张三'), ('', '男', '', '张一'), ('', '女', '', '张二'), ('', '男', '', '张四'), ('', '女', '', '铁锤'), ('', '男', '', '李三'), ('', '男', '', '李一'), ('', '女', '', '李二'), ('', '男', '', '李四'), ('', '女', '', '如花'), ('', '男', '', '刘三'), ('', '男', '', '刘一'), ('', '女', '', '刘二'), ('', '男', '', '刘四');
COMMIT; -- ----------------------------
-- Table structure for `teacher`
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
`tid` INT(11) NOT NULL AUTO_INCREMENT,
`tname` VARCHAR(32) NOT NULL,
PRIMARY KEY (`tid`)
) ENGINE=INNODB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `teacher`
-- ----------------------------
BEGIN;
INSERT INTO `teacher` VALUES ('', '张磊老师'), ('', '李平老师'), ('', '刘海燕老师'), ('', '朱云海老师'), ('', '李杰老师');
COMMIT; SET FOREIGN_KEY_CHECKS = 1;
2、sql练习
/*
# 生物分数大于物理分数的学生学号
获取所有有生物课程的人(学号,成绩) - 临时表
获取所有有物理课程的人(学号,成绩) - 临时表
根据【学号】连接两个临时表:
学号 物理成绩 生物成绩
select a.student_id, a.num as 'sw', b.num as 'wl' from
(select * from score where course_id in (select cid from course where cname = '生物')) a
left join
(SELECT * FROM score WHERE course_id IN (SELECT cid FROM course WHERE cname = '物理')) b
on a.student_id = b.student_id where a.num > b.num
select A.student_id,sw,ty from
(select student_id,num as sw from score left join course on score.course_id = course.cid where course.cname = '生物') as A
left join
(select student_id,num as ty from score left join course on score.course_id = course.cid where course.cname = '体育') as B
on A.student_id = B.student_id where sw > if(isnull(ty),0,ty);
*/
# 查询平均成绩大于60分的同学的学号和平均成绩;
-- select student_id, avg(num) from score group by student_id having avg(num) > 60
# 查询所有同学的学号、姓名、选课数、总成绩
-- select a.student_id, b.sname, count(a.course_id), sum(a.num) from score a
-- left join student b on a.student_id = b.sid
-- GROUP BY a.student_id
# 查询姓“李”的老师的个数
-- select count(tid) from teacher where tname like "李%"
-- ?? select count(1) from (select tid from teacher where tname like '李%') as B
/*
# 查询没学过“李平”老师课的同学的学号、姓名
select sid,sname from student where sid not in (
select student_id from score
where course_id IN
(select cid from course where teacher_id in (SELECT tid FROM teacher WHERE tname = '李平老师')))
# DISTINCT 字段进行去重处理,符合预期期望
select * from student where sid not in (
select DISTINCT student_id from score where score.course_id in (
select cid from course left join teacher on course.teacher_id = teacher.tid where tname = '李平老师'
))
*/
/*
# 查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
思路:
先查到既选择001又选择002课程的所有同学
根据学生进行分组,如果学生数量等于2表示,两门均已选择
select a.sid, a.sname,b.num from
(select student_id, count(course_id) as num from score where course_id = 1 or course_id = 2 GROUP BY student_id) b
left join student a on b.student_id = a.sid where b.num = 2;
select student_id,sname from
(select student_id,course_id from score where course_id = 1 or course_id = 2) as B
left join student on B.student_id = student.sid group by student_id HAVING count(student_id) > 1
*/
/*
查询学过“叶平”老师所教的所有课的同学的学号、姓名;
同上,只不过将001和002变成 in (叶平老师的所有课)
*/
/*
查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
同第1题
*/
/*
查询有课程成绩小于60分的同学的学号、姓名;
select sid,sname from student where sid in (
select distinct student_id from score where num < 60
)
*/
/*查询没有学全所有课的同学的学号、姓名;
思路:
在分数表中根据学生进行分组,获取每一个学生选课数量
如果数量 == 总课程数量,表示已经选择了所有课程
select sid,sname from student where sid not in (
select student_id from score group by student_id having COUNT(course_id) =
(select count(cid) from class))
select student_id,sname
from score left join student on score.student_id = student.sid
group by student_id HAVING count(course_id) = (select count(1) from course)
*/
/*
查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
思路:
获取 001 同学选择的所有课程
获取课程在其中的所有人以及所有课程
根据学生筛选,获取所有学生信息
再与学生表连接,获取姓名
select student_id, sname, count(course_id)
from score left join student on score.student_id = student.sid
where student_id != 1 and course_id in (select course_id from score where student_id = 1) group by student_id;
*/
/*
查询至少学过学号为“001”同学所有课的其他同学学号和姓名;
先找到和001的学过的所有人
然后个数 = 001所有学科 ==》 其他人可能选择的更多
select student_id,sname, count(course_id)
from score left join student on score.student_id = student.sid
where student_id != 1 and course_id in (select course_id from score where student_id = 1)
group by student_id having count(course_id) >= (select count(course_id) from score where student_id = 1)
*/
/*
查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;
个数相同
002学过的也学过
select student_id,sname from score left join student on score.student_id = student.sid where student_id in (
select student_id from score where student_id != 1 group by student_id
HAVING count(course_id) = (select count(1) from score where student_id = 1))
and course_id in (select course_id from score where student_id = 1) group by student_id
HAVING count(course_id) = (select count(1) from score where student_id = 1)
*/
/*
删除学习“叶平”老师课的score表记录;
delete from score where course_id in (
select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.name = '叶平'
)
*/
/*
向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;
思路:
由于insert 支持
inset into tb1(xx,xx) select x1,x2 from tb2;
所有,获取所有没上过002课的所有人,获取002的平均成绩
insert into score(student_id, course_id, num) select sid,2,(select avg(num) from score where course_id =2)
from student where sid not in (select student_id from score where course_id = 2)
*/
/*
按平均成绩从低到高 显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示:
学生ID,语文,数学,英语,有效课程数,有效平均分;
select sc.student_id,
(select num from score left join course on score.course_id = course.cid where course.cname = "生物" and score.student_id=sc.student_id) as sy,
(select num from score left join course on score.course_id = course.cid where course.cname = "物理" and score.student_id=sc.student_id) as wl,
(select num from score left join course on score.course_id = course.cid where course.cname = "体育" and score.student_id=sc.student_id) as ty,
count(sc.course_id),
avg(sc.num)
from score as sc
group by student_id order by AVG(sc.num) asc;
*/
/*
查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
SELECT course_id, MAX(num) AS max_num, MIN(num) AS min_num FROM score GROUP BY course_id;
*/
/*
按各科平均成绩从低到高和及格率的百分数从高到低顺序;
思路:case when .. then
SELECT course_id, AVG(num) AS avgnum,SUM(CASE WHEN score.num > 60 THEN 1 ELSE 0 END)/COUNT(1)*100 AS percent
FROM score GROUP BY course_id ORDER BY avgnum ASC,percent DESC;
*/
/*
课程平均分从高到低显示(现实任课老师);
select avg(if(isnull(score.num),0,score.num)),teacher.tname from course left join score on course.cid = score.course_id left join teacher on course.teacher_id = teacher.tid group by score.course_id/*查询各科成绩前三名的记录:(不考虑成绩并列情况) select score.sid,score.course_id,score.num,T.first_num,T.second_num from score left join ( select sid, (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num, (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 3,1) as second_num from score as s1 ) as T on score.sid =T.sid where score.num <= T.first_num and score.num >= T.second_num查询每门课程被选修的学生数; select course_id, count(1) from score group by course_id;/*查询出只选修了一门课程的全部学生的学号和姓名; select student.sid, student.sname, count(1) from score left join student on score.student_id = student.sid group by course_id having count(1) = 1/*查询男生、女生的人数; select * from (select count(1) as man from student where gender='男') as A , (select count(1) as feman from student where gender='女') as B查询姓“张”的学生名单; select sname from student where sname like '张%';查询同名同姓学生名单,并统计同名人数; select sname,count(1) as count from student group by sname;查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列; select course_id,avg(if(isnull(num), 0 ,num)) as avg from score group by course_id order by avg asc,course_id desc;查询平均成绩大于85的所有学生的学号、姓名和平均成绩; select student_id,sname, avg(if(isnull(num), 0 ,num)) from score left join student on score.student_id = student.sid group by student_id;查询课程名称为“数学”,且分数低于60的学生姓名和分数; select student.sname,score.num from score left join course on score.course_id = course.cid left join student on score.student_id = student.sid where score.num < 60 and course.cname = '生物'查询课程编号为003且课程成绩在80分以上的学生的学号和姓名; select * from score where score.student_id = 3 and score.num > 80/*
求选了课程的学生人数
*/
SELECT COUNT(DISTINCT student_id) AS c FROM score
/*
查询选修“杨艳”老师所授课程的学生中,成绩最高的学生姓名及其成绩;
*/
SELECT sname,num FROM score
LEFT JOIN student ON score.student_id = student.`sid`
WHERE score.course_id IN
(SELECT course.cid FROM course LEFT JOIN teacher ON course.`teacher_id`= teacher.tid WHERE teacher.tname='张磊老师')
ORDER BY num DESC LIMIT 1;
/*
查询各个课程及相应的选修人数;
*/
SELECT a.course_id, b.`cname`,COUNT(1) FROM score AS a INNER JOIN course AS b ON a.`course_id`= b.`cid` GROUP BY course_id;
/*
查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
*/
SELECT DISTINCT s1.`student_id`,s2.`student_id`,s1.course_id,s2.course_id,s1.num,s2.num
FROM score AS s1, score AS s2
WHERE s1.num = s2.num AND s1.`student_id` != s2.`student_id`;
SELECT a.`student_id`,cc.`student_id`,a.course_id,cc.course_id,a.num,cc.num
FROM score AS a
INNER JOIN (SELECT student_id,course_id,num FROM score) AS cc ON a.num=cc.num AND a.student_id!= cc.student_id
/*
查询每门课程成绩最好的前两名;
*/
SELECT score.sid,score.course_id,T.first_num,T.second_num FROM score LEFT JOIN
(
SELECT
sid,
(SELECT num FROM score AS s2 WHERE s2.course_id = s1.course_id ORDER BY num DESC LIMIT 0,1) AS first_num,
(SELECT num FROM score AS s2 WHERE s2.course_id = s1.course_id ORDER BY num DESC LIMIT 1,1) AS second_num
FROM
score AS s1
) AS T
ON score.sid =T.sid
WHERE score.num <= T.first_num AND score.num >= T.second_num
/*
检索至少选修两门课程的学生学号;
*/
SELECT student_id FROM score GROUP BY student_id HAVING COUNT(student_id) > 1
/*
查询全部学生都选修的课程的课程号和课程名;
*/
SELECT course_id,COUNT(1) FROM score GROUP BY course_id HAVING COUNT(1) = (SELECT COUNT(1) FROM student);
/*
查询没学过“叶平”老师讲授的任一门课程的学生姓名;
*/
SELECT student_id,student.sname FROM score
LEFT JOIN student ON score.student_id = student.sid
WHERE score.course_id NOT IN (
SELECT cid FROM course LEFT JOIN teacher ON course.teacher_id = teacher.tid WHERE tname = '张磊老师'
)
GROUP BY student_id
/*
查询两门以上不及格课程的同学的学号及其平均成绩;
*/
SELECT student_id,COUNT(1) FROM score WHERE num < 60 GROUP BY student_id HAVING COUNT(1) > 2
/*
检索“004”课程分数小于60,按分数降序排列的同学学号;
*/
SELECT student_id FROM score WHERE num< 60 AND course_id = 4 ORDER BY num DESC;
/*
删除“002”同学的“001”课程的成绩;
*/
DELETE FROM score WHERE course_id = 1 AND student_id = 2
mysql应用实例的更多相关文章
- MYSQL多实例配置方法 mysqld_multi方法
在实际的开发过程中,可能会需要在一台服务器上部署多个MYSQL实例,那建议使用MYSQL官方的解决方案 mysqld_multi 1.修改my.cnf 如一个定义两个实例的参考配置: [mysqld_ ...
- MySQL优化实例
这周就要从泰笛离职了,在公司内部的wiki上,根据公司实际的项目,写了一些mysql的优化方法,供小组里的小伙伴参考下,没想到大家的热情很高,还专门搞了个ppt讲解了一下. 举了三个大家很容易犯错的地 ...
- mysql 优化实例之索引创建
mysql 优化实例之索引创建 优化前: pt-query-degist分析结果: # Query 23: 0.00 QPS, 0.00x concurrency, ID 0x78761E301CC7 ...
- PHP调用MYSQL存储过程实例
PHP调用MYSQL存储过程实例 标签: mysql存储phpsqlquerycmd 2010-09-26 11:10 11552人阅读 评论(3) 收藏 举报 实例一:无参的存储过程$conn = ...
- JDBC连接MySQL 方法 实例及资料收集
JDBC连接MySQL 方法 实例及资料收集 准备工作 首先,安装MySQL,配置用户名和密码,创建数据库. 可参见之前的文章: http://www.cnblogs.com/mengdd/p/315 ...
- mysql多实例的配置和管理
原文地址:mysql多实例的配置和管理 作者:飞鸿无痕 多实例mysql的安装和管理 mysql的多实例有两种方式可以实现,两种方式各有利弊.第一种是使用多个配置文件启动不同的进程来实现多实例,这种方 ...
- MySQL多实例,主从同步
由于背景原因,所做的主从同步还是要基于MySQL 5.1的版本,主从同步主要是一个数据库读写访问原来的数据库热度过大,需要做到使用从库对读分压. MySQL主从同步介绍 MySQL 支持单双向 ...
- mysql多实例(个人的情况,不是大众的)里面有配置好的脚本+主从复制
[root@DB-S ~]# ll /usr/local/|grep mysql lrwxrwxrwx. 1 root root 21 Jun 14 01:52 mysql -> /alidat ...
- mysql多实例部署
mysql 多实例常规来讲,主要有二种方案可以实现,这二种方案各有利弊,如下:1.基于多配置文件 通过使用多个配置文件来启动不同的进程,以此来实现多实例. 优点:逻辑简单,配置简单 缺点:管理起来不方 ...
- 调用MYSQL存储过程实例
PHP调用MYSQL存储过程实例 http://blog.csdn.net/ewing333/article/details/5906887 http://www.cnblogs.com/kkchen ...
随机推荐
- 01 awk工具的使用
一:登录mysql后查看mysql的连接状态:show status ; 回车 如图所示: |Threads_connected | 1| Threads_running | 1 ...
- GAN 生成mnist数据
参考资料 GAN原理学习笔记 生成式对抗网络GAN汇总 GAN的理解与TensorFlow的实现 TensorFlow小试牛刀(2):GAN生成手写数字 参考代码之一 #coding=utf-8 #h ...
- Unity3D项目之 Survival Shooter 记录
1.导入资源 2.把预设文件的环境拖到场景中, 3.位置归0 4.保存场景 5.删除默认灯光,把预设灯光拖到场景中,位置归0 6.新建一个 Quad 7.旋转90度,设置缩放100,100,1 重命名 ...
- 【BZOJ】1681: [Usaco2005 Mar]Checking an Alibi 不在场的证明(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=1681 太裸了.. #include <cstdio> #include <cstr ...
- c#方法生成mysql if方法(算工作日)
public static string retunSQl(string s,string e){ return @"IF ( "+s+ ">" +e+ ...
- Angular2 表单(一) 用户输入
绑定到用户输入事件 等号左边的 (click) 表示把按钮的点击事件作为绑定目标. 等号右边引号中的文本是模板语句,通过调用组件的 onClickMe 方法来响应这个点击事件. <button ...
- 运维角度浅谈:MySQL数据库优化
日志君导读: 一个成熟的数据库架构并非一開始设计就具备高可用.高伸缩等特性的.它是随着用户量的添加,基础架构才逐渐完好. 作者:zhenliang8.本文转自51CTO博客,点击原文阅读查看网页版文章 ...
- 【javaScript基础】马上调用函数表达式
在javaScript中,每一个函数被调用时,都会创建一个新的运行上下文.由于在一个函数里面定义的变量和函数仅仅能在里面訪问.在外面是不行的.上下文提供了一种非常easy的方法来创建私有性. //ma ...
- malloc free, new delete 的异同点
相同点: 都可以动态的申请并释放内存 不同点: 1. 用法不同 <1> malloc 函数为 void* malloc(size_t size), 用于申请一块长度为 size 字节的内存 ...
- linux C之alarm函数 转
原文出处:http://blog.sina.com.cn/s/blog_6a1837e90100uhl3.html alarm也称为闹钟函数,alarm()用来设置信号SIGALRM在经过参数seco ...