第四天(41道题全解)

41道题:

重要的
连表查询
分组

制表语句:
    班级表
               Table: class
        Create Table: CREATE TABLE `class` (
          `cid` int(11) NOT NULL AUTO_INCREMENT,
          `caption` varchar(50) DEFAULT NULL,
          PRIMARY KEY (`cid`)
        ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
        1 row in set (0.00 sec)

    学生表:
                       Table: student
        Create Table: CREATE TABLE `student` (
          `sid` int(11) NOT NULL AUTO_INCREMENT,
          `sname` varchar(50) DEFAULT NULL,
          `gender` char(10) DEFAULT NULL,
          `class_id` int(11) DEFAULT NULL,
          PRIMARY KEY (`sid`),
          KEY `fk_student_class` (`class_id`),
          CONSTRAINT `fk_student_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
        ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

    老师表:
                       Table: teacher
        Create Table: CREATE TABLE `teacher` (
          `tid` int(11) NOT NULL AUTO_INCREMENT,
          `tname` varchar(50) DEFAULT NULL,
          PRIMARY KEY (`tid`)
        ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
        1 row in set (0.00 sec)

    课程表:
                      Table: course
        Create Table: CREATE TABLE `course` (
          `cid` int(11) NOT NULL AUTO_INCREMENT,
          `cname` char(25) DEFAULT NULL,
          `teacher_id` int(11) DEFAULT 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=4 DEFAULT CHARSET=utf8
        1 row in set (0.00 sec)

    成绩表:    

        create table score
             (sid int not null auto_increment primary key,
              student_id int not null,
             corse_id int not null,
             number int not null,
             unique uq_sc (student_id,corse_id),

             CONSTRAINT fk_sc_st FOREIGN key (student_id) REFERENCES student(sid),
             constraint fk_sc_co foreign key  (corse_id) references course(cid)
             ) engine=innodb default charset=utf8;

    group by前面的slect 只能用聚合函数count,max什么的     

v= 111 if 1==1 else 110 三元运算符
#########################################################
作业练习:
    http://www.cnblogs.com/wupeiqi/articles/5729934.html    

http://www.cnblogs.com/wupeiqi/p/5748496.html  参考答案            

##########################################################

用到的知识点
- 临时表
        select * from (select * from tb where id< 10) as B;

    ---
        select
            id,
            name,
            1,
            (select count(1) from tb)----只要拿到的是一个值就可以
        from tb2
    ---把最外层的表s1,每次循环取一个常量值再到每一列中再循环一次
        SELECT
            student_id,
            (select num from score as s2 where s2.student_id=s1.student_id and course_id = 1) as 语文,
            (select num from score as s2 where s2.student_id=s1.student_id and course_id = 2) as 数学,
            (select num from score as s2 where s2.student_id=s1.student_id and course_id = 3) as 英语
        from score as s1;            

    ----avg(if(isnull(score.num),0,score.num))
         三目运算符

#####################################################
具体作业:
    1、自行创建测试数据

2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
    PS:查出生物课程的学生,查出物理课程的学生,用临时表,再连接查询
        select A.student_id from
        (select * from score
        LEFT JOIN course on score.course_id=course.cid
        where course.cname="生物"
        ) as A
        LEFT JOIN
        (select * from score
        LEFT JOIN course on score.course_id=course.cid
        where course.cname="物理"
        ) as B
        on A.student_id=B.student_id
        where A.num> B.num

3、查询平均成绩大于60分的同学的学号和平均成绩;
    select student_id, avg(num) from score GROUP BY student_id
    PS:把名字也带上
    select S.student_id,student.sname,S.av_s from
    (SELECT student_id, avg(num) AS av_s FROM score GROUP BY student_id HAVING av_s > 60) as S
    LEFT JOIN student on student.sid=S.student_id

4、查询所有同学的学号、姓名、选课数、总成绩;
    SELECT score.student_id,student.sname, COUNT(score.student_id),sum(score.num) from score
    LEFT JOIN student ON score.student_id=student.sid
    GROUP BY score.student_id

5、查询姓“李”的老师的个数;
    select COUNT(*) from teacher where teacher.tname
    LIKE "李%"

6、查询没学过“李平老师“课的同学的学号、姓名;
    PS:先查找李平的课程ID,再到score表中找学他的课的学生Id,再去student表中全部排除
    select student.sid,student.sname from student
    where student.sid not in
    (
    select score.student_id from score
    where course_id in (select course.cid from course
    LEFT JOIN teacher on teacher.tid=course.cid
    where teacher.tname="李平老师")
    )

7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
    PS:找出score表中学过课程1,或者2,或者1,2的学生,分组找出大于2的,再与学生表进行连接查询
    select student.sid,student.sname from score
    LEFT JOIN  student on score.student_id=student.sid
     where score.course_id=1 or score.course_id=2 GROUP BY score.student_id HAVING count(score.course_id)>1

8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
    PS:语句分两部分B表是查出学过叶平老师所有课程的学生ID(注意其中叶平的课程有所变化所以不能count >2)
         再与学生表进行关联
    SELECT student.sid,student.sname FROM student
    LEFT JOIN
        (
            SELECT score.student_id FROM score WHERE score.course_id
                IN (
                    SELECT cid FROM course
                    LEFT JOIN teacher ON teacher.tid = course.teacher_id
                    WHERE teacher.tname = "李平老师"
                   ) --查出叶平老师所教的课程的ID
            GROUP BY score.student_id
            HAVING
                count(score.student_id) =
                (
                    SELECT count(cid) FROM course LEFT JOIN teacher ON teacher.tid = course.teacher_id
                    WHERE teacher.tname = "李平老师"
                )        --查出叶平老师总共教了几门课count(cid)
        ) AS B             --查出学过叶平老师所有课程的学生ID
    ON student.sid = B.student_id

9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
    PS:
        select student.sid,student.sname from student
        left JOIN
            (select A.student_id from
                    (select * from score
                    LEFT JOIN course on score.course_id=course.cid
                    where course.cid=1
                    ) as A
                    LEFT JOIN
                    (select * from score
                    LEFT JOIN course on score.course_id=course.cid
                    where course.cid=2
                    ) as B
                    on A.student_id=B.student_id
                    where A.num<B.num
            ) as C
        on student.sid=C.student_id

10、查询有课程成绩小于60分的同学的学号、姓名;
        select sid,sname from student where sid in (
        select distinct student_id from score where num < 60
    )

11、查询没有学全所有课的同学的学号、姓名;
    PS ---count要嘛用1要嘛用主键,效率高
    SELECT student.sid,student.sname from student
    INNER JOIN
     (
        select score.student_id,count(1) from score GROUP BY score.student_id HAVING count(1) < (SELECT count(cid) from course)
     ) as B
    on student.sid = B.student_id

12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
    select student.sid,student.sname from student
    INNER JOIN
    (
    SELECT score.student_id,score.course_id from score where score.student_id !=1
    and  score.course_id in (select score.course_id from score where score.student_id=1) GROUP BY score.student_id
    ) as B
    on student.sid=B.student_id

13、查询至少学过学号为“001”同学所有课的其他同学学号和姓名
    select student.sid,student.sname from student
    INNER  JOIN
    (
        SELECT score.student_id,count(1) from score where score.student_id !=1
            and  score.course_id in (select score.course_id from score where score.student_id=1)
        GROUP BY score.student_id
        HAVING count(1) = (select count(1) from score where score.student_id=1)
        ) AS B
    on student.sid=B.student_id

14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;
    SELECT student_id from score where student_id
    in (
        select student_id from score where student_id !=2 GROUP BY student_id
            HAVING COUNT(1) =(select count(1) from score where student_id=1)
        )
    and course_id in (SELECT course_id from score where student_id =2)
    GROUP BY student_id HAVING COUNT(1) = (SELECT count(1) from score where student_id)

15、删除学习“叶平”老师课的SC表记录;
    delete from score where course_id in
    (
      select cid from course left join teacher on course.teacher_id = teacher.tid where teacher.name = ‘叶平‘
    )

16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;
    insert into score(student_id,course_id,num)
    VALUES(select student_id,2,(select AVG(num) from score where course_id = 2) from score where course_id !=2) 

17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
       select s1.student_id,
       (select num from score left join course on score.course_id = course.cid where course.cname = "生物" and score.student_id=s1.student_id) as 生物,
        (select num from score left join course on score.course_id = course.cid where course.cname = "物理" and score.student_id=s1.student_id) as 物理,
        (select num from score left join course on score.course_id = course.cid where course.cname = "体育" and score.student_id=s1.student_id) as 体育,
        count(s1.course_id),
        avg(s1.num)
    from score as s1
    group by student_id desc

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
    SELECT course_id,MAX(num),min(num) from score GROUP BY course_id

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;

            select
        course_id,avg(num),
        sum(case when num <60 THEN 0 ELSE 1 END),sum(1),
        sum(case when num <60 THEN 0 ELSE 1 END)/sum(1) as jgl
        from score GROUP BY course_id order by AVG(num) asc,jgl desc;

20、课程平均分从高到低显示(现实任课老师);
        select
        course_id,avg(num),teacher.tname
        from score
        LEFT JOIN course on score.course_id=course.cid
        left JOIN teacher on teacher.tid = course.teacher_id
        GROUP BY course_id 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

21、查询各科成绩前三名的记录:(不考虑成绩并列情况) 

22、查询每门课程被选修的学生数;
SELECT student_id ,count(1) from score GROUP BY course_id HAVING COUNT(1) >5

23、查询出只选修了一门课程的全部学生的学号和姓名;
SELECT  student.sid,student.sname from student
INNER  JOIN
(SELECT student_id ,count(1) from score GROUP BY student_id HAVING COUNT(1) =1) as B
on student.sid=B.student_id

24、查询男生、女生的人数;
SELECT gender,COUNT(1) from student GROUP BY gender

25、查询姓“张”的学生名单;
SELECT sname from student where sname LIKE "张%"

26、查询同名同姓学生名单,并统计同名人数;
SELECT sname,count(1) from student GROUP BY sname

27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
SELECT course_id,AVG(num) from score GROUP BY score.course_id ORDER BY avg(num) ASC,course_id DESC

28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;
SELECT sid,sname,T.B from student
INNER  JOIN
(SELECT student_id ,avg(if(ISNULL(num) ,0,num)) as B from score GROUP BY student_id HAVING B >85) as T
on T.student_id=student.sid

29、查询课程名称为“生物”,且分数低于60的学生姓名和分数;
    SELECT student.sname,score.num from course
    LEFT JOIN score on course.cid=score.course_id
    LEFT JOIN student on student.sid=score.student_id

    where course.cname=‘生物‘ and score.num < 60

30、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;

    SELECT student.sname,score.num from course
    LEFT JOIN score on course.cid=score.course_id
    LEFT JOIN student on student.sid=score.student_id
    where course.cid=3 and score.num >80 

31、求选了课程的学生人数
SELECT count(DISTINCT(score.student_id)) from score

32、查询选修“李平”老师所授课程的学生中,成绩最高的学生姓名及其成绩;

SELECT student.sname,T.num from student
INNER  JOIN
(SELECT * from teacher
LEFT JOIN course on teacher.tid=course.teacher_id
LEFT JOIN score on score.course_id=course.cid
 GROUP BY num HAVING teacher.tname="张磊老师"  ORDER BY  num DESC LIMIT 0,1) as T
on student.sid= T.student_id

33、查询各个课程及相应的选修人数;
 select course.cname,count(1) from score
    left join course on score.course_id = course.cid
    group by course_id;

34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
SELECT DISTINCT s1.student_id,s1.course_id,s1.num from score as s1,score as s2 WHERE s1.sid !=s2.sid and s1.course_id !=s2.course_id and s1.num=s2.num

35、查询每门课程成绩最好的前两名;
 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 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

36、检索至少选修两门课程的学生学号;
SELECT student_id from score GROUP BY student_id HAVING count(student_id) >1

37、查询全部学生都选修的课程的课程号和课程名;

38、查询没学过“叶平”老师讲授的任一门课程的学生姓名;
    PS:先查出学过李平老师任意一门课的学生,包括只选了一门的,然后,全部排除
    SELECT student.sid from student
    WHERE sid not in(
    SELECT score.student_id from score
    WHERE course_id in
    (
    SELECT  course.cid from course
     LEFT JOIN teacher on course.teacher_id=teacher.tid WHERE teacher.tname="李平老师"
     ))

39、查询两门以上不及格课程的同学的学号及其平均成绩;
 select student_id,count(1) from score where num < 60 group by student_id having count(1) > 2

40、检索“004”课程分数小于60,按分数降序排列的同学学号;
   select student_id from score where num< 60 and course_id = 4 order by num desc;

41、删除“002”同学的“001”课程的成绩;
    delete from score where course_id = 1 and student_id = 2

时间: 2024-10-19 05:52:39

第四天(41道题全解)的相关文章

PHP漏洞全解(四)-xss跨站脚本攻击

本文主要介绍针对PHP网站的xss跨站脚本攻击.跨站脚本攻击是通过在网页中加入恶意代码,当访问者浏览网页时恶意代码会被执行或者通过给管理员发信息 的方式诱使管理员浏览,从而获得管理员权限,控制整个网站.攻击者利用跨站请求伪造能够轻松地强迫用户的浏览器发出非故意的HTTP请求,如诈骗性的电汇 请求.修改口令和下载非法的内容等请求. XSS(Cross Site Scripting),意为跨网站脚本攻击,为了和样式表css(Cascading Style Sheet)区别,缩写为XSS 跨站脚本主要

Sql Server函数全解&lt;四&gt;日期和时间函数

原文:Sql Server函数全解<四>日期和时间函数   日期和时间函数主要用来处理日期和时间值,本篇主要介绍各种日期和时间函数的功能和用法,一般的日期函数除了使用date类型的参数外,也可以使用datetime类型的参数,但会忽略这些值的时间部分.相同的,以time类型值为参数的函数,可以接受datetime类型的参数,但会忽略日期部分. 1.获取系统当前日期的函数getDate();  getDate()函数用于返回当前数据库系统的日期和时间,返回值的类型为datetime.[例]sel

“全栈2019”Java第五十四章:多态详解

难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第五十四章:多态详解 下一章 "全栈2019"Java第五十五章:方法的静态绑定与动态绑定 学习小组 加入同步学习小组,共同交流与进步. 方式一:关注头条号Gorhaf,私信"Java学习小组". 方式二:关注公众号Gorhaf,回复"Java学习小组"

UITextField 全解

IOS-UITextField-全解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)]; //设置边框样式,只有设置了才会显示边框样式 text.borderStyle = UITextBorderStyleRoundedRect; typedef enum { UITextBorderStyleNone, UITextBorderSt

RAM和yum功能全解

一RPM程序包管理器功能全解 1.RPM简介 RPM全称为"RedHat Package Manager",是有RedHat公司开发的程序包管理器.RPM 是以一种数据库记录的方式将所需要的套件安装到Linux主机的一套程序包管理器. RPM的命名格式: Name-version-release.arch.rpm 套件名称  套件的版本 发行号 适合的硬件平台 扩展名 RPM的组成清单: 1文件清单 2安装或卸载时运行的脚本 3数据库(公共) 4程序包的名称和版本 5依赖关系 6功能说

Fortify SCA 分析代码漏洞全解

上次介绍了用FindBugs辅助分析代码漏洞,这次换了一个工具:Fortify SCA Demo 4.0.0.Fortify是一个在安全方面挺出名的公司,这里就不多说了.先介绍一下主角:Fortify SCA Demo 4.0.0,虽然现在不知道Fortify SCA的版本是多少,但可以肯定的是,Fortify SCA Demo 4.0.0是一个比较旧的Fortify SCA分析器了,并且还是Demo版的,所以无论是界面还是功能上都是比较简陋的.由于Fortify SCA不是开源的工具,这里就不

rpm包管理功能全解

rpm包管理功能全解            linux rpm问题:怎样查看rpm安装包的安装路径                     rpm -qa l grep  xxxxxx.rpm                    rpm的命令:rpm  [OPTIONS]  [PACKAGE_FILE]            安装:-i, --install            升级:-U, --update, -F, --freshen            卸载:-e, --erase 

POST &amp;amp; GET &amp;amp; Ajax 全解

GET&POST&Ajax 全解 一.POST和GET的差别 GET:GET方法提交数据不安全,数据置于请求行.客户段地址栏可见:GET方法提交的数据限制大小在255个字符之内.參数直接跟在URL后面清晰可见,该http请求的body部分也是空的.仅仅有head部分显示了一个http的基本信息. POST:POST方法提交的数据置于消息主体内,client不可见,POST提交的数据大小没有限制. POST方式发送的http请求,參数不是跟在URL后面的,而是存放在http请求的body部分

MySQL复制异常大扫盲:快速溯源与排查错误全解

MySQL复制异常大扫盲:快速溯源与排查错误全解https://mp.weixin.qq.com/s/0Ic8BnUokyOj7m1YOrk1tA 作者介绍王松磊,现任职于UCloud,从事MySQL数据库内核研发工作,主要负责UCloud云数据库UDB的内核故障排查工作以及数据库新特性的研发工作. 复制作为MySQL原生的数据同步功能,在MySQL高可用架构中起着至关重要的作用.本文梳理了MySQL高可用产品UDB在日常运维中遇到的复制问题,并总结了当复制发生异常时,排查复制异常的方法. 一.