目录:

  • 表结构
  • 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应用实例的更多相关文章

  1. MYSQL多实例配置方法 mysqld_multi方法

    在实际的开发过程中,可能会需要在一台服务器上部署多个MYSQL实例,那建议使用MYSQL官方的解决方案 mysqld_multi 1.修改my.cnf 如一个定义两个实例的参考配置: [mysqld_ ...

  2. MySQL优化实例

    这周就要从泰笛离职了,在公司内部的wiki上,根据公司实际的项目,写了一些mysql的优化方法,供小组里的小伙伴参考下,没想到大家的热情很高,还专门搞了个ppt讲解了一下. 举了三个大家很容易犯错的地 ...

  3. mysql 优化实例之索引创建

    mysql 优化实例之索引创建 优化前: pt-query-degist分析结果: # Query 23: 0.00 QPS, 0.00x concurrency, ID 0x78761E301CC7 ...

  4. PHP调用MYSQL存储过程实例

    PHP调用MYSQL存储过程实例 标签: mysql存储phpsqlquerycmd 2010-09-26 11:10 11552人阅读 评论(3) 收藏 举报 实例一:无参的存储过程$conn = ...

  5. JDBC连接MySQL 方法 实例及资料收集

    JDBC连接MySQL 方法 实例及资料收集 准备工作 首先,安装MySQL,配置用户名和密码,创建数据库. 可参见之前的文章: http://www.cnblogs.com/mengdd/p/315 ...

  6. mysql多实例的配置和管理

    原文地址:mysql多实例的配置和管理 作者:飞鸿无痕 多实例mysql的安装和管理 mysql的多实例有两种方式可以实现,两种方式各有利弊.第一种是使用多个配置文件启动不同的进程来实现多实例,这种方 ...

  7. MySQL多实例,主从同步

    由于背景原因,所做的主从同步还是要基于MySQL 5.1的版本,主从同步主要是一个数据库读写访问原来的数据库热度过大,需要做到使用从库对读分压. MySQL主从同步介绍     MySQL 支持单双向 ...

  8. mysql多实例(个人的情况,不是大众的)里面有配置好的脚本+主从复制

    [root@DB-S ~]# ll /usr/local/|grep mysql lrwxrwxrwx. 1 root root 21 Jun 14 01:52 mysql -> /alidat ...

  9. mysql多实例部署

    mysql 多实例常规来讲,主要有二种方案可以实现,这二种方案各有利弊,如下:1.基于多配置文件 通过使用多个配置文件来启动不同的进程,以此来实现多实例. 优点:逻辑简单,配置简单 缺点:管理起来不方 ...

  10. 调用MYSQL存储过程实例

    PHP调用MYSQL存储过程实例 http://blog.csdn.net/ewing333/article/details/5906887 http://www.cnblogs.com/kkchen ...

随机推荐

  1. vmware无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件

    原因: 是虚拟机服务没有开启 解决方法:(以管理员的方式运行) 点击“开始→运行”,在运行框中输入 CMD  回车打开命令提示符,然后依次执行以下命令. net start vmcinet start ...

  2. nginx的 keepalive_timeout参数是一个请求完成之后还要保持连

    keepalive_timeout参数是一个请求完成之后还要保持连接多久,不是请求时间多久,目的是保持长连接,减少创建连接过程给系统带来的性能损耗,类似于线程池,数据库连接池. [root@web01 ...

  3. 单引號转义符q’的使用

    当字符串包括单引號时,能够使用转义符q'对单引號进行转义. q'后面的字符能够是:     !     [ ]     { }     ( )     < > 前提是这些字符不会出如今兴许 ...

  4. 经典SQL基础回顾

    孔子有云:温故而知新,可以为师矣.既然孔老圣人都云了,咱今天就一起来重温一下MS SQL吧.开篇声明一下:大部分都是基础内容,SQL非常熟练的就别浪费您的时间了,因为这年头,大家时间都挺宝贵的.但是如 ...

  5. Maven的Settings.xml配置文件解释

    该配置用于单用户配置和全局配置, 单用户配置默认存放于 ${user.home}/.m2/目录中. 全局配置默认存放于Maven安装目录下面的conf目录中. 这两个默认的位置都可以修改. <? ...

  6. Spark源代码阅读笔记之MetadataCleaner

    MetadataCleaner执行定时任务周期性的清理元数据(metadata),有6种类型的元数据:MAP_OUTPUT_TRACKER.executor跟踪各个map任务输出的存储位置的数据,依据 ...

  7. iOS开发之--当遇到tableView整体上移时的解决方案

    方案一在使用了navigationController后,当界面进行跳转往返后,时而会出现tableView上移的情况,通常会自动上移64个像素,那么这种情况,我们可以关闭tableView的自动适配 ...

  8. Spring中bean的作用范围

    singleton作用域: Spring的scope的默认值是singleton Spring 只会为每一个bean创建一个实例,并保持bean的引用. <bean id="bean的 ...

  9. 点击input选中文本

    选文本: $(".unline-ipt").click(function () { $(this).focus().select(); this.selectionStart = ...

  10. CSS解决图片缩小不变形

    我会在图片上加: <img style="max-width:80px;max-height:80px;"> 限制其最大宽度和高度