比较复杂的数据库查询案例,建表语句和测试数据[通俗易懂]

比较复杂的数据库查询案例,建表语句和测试数据[通俗易懂]比较复杂的数据库查询案例,建表语句和测试数据

大家好,又见面了,我是你们的朋友全栈君。

create DATABASE test
CREATE TABLE Student ( S VARCHAR(64), Sname VARCHAR(64) ,Sage INT, Ssex VARCHAR(2), PRIMARY KEY (S) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE Course ( C VARCHAR(64),Cname VARCHAR(64), T VARCHAR(64) ) engine=InnoDb DEFAULT charset=utf8;
CREATE TABLE SC ( S VARCHAR(64), C VARCHAR(64), score INT) engine=InnoDB DEFAULT CHARSET=UTF8;
CREATE TABLE Teacher ( T VARCHAR(64), Tname VARCHAR(64) ) engine=Innodb DEFAULT charset=utf8;

insert into test.course (C,Cname,T) VALUES('001','数学','001');
insert into test.course (C,course.Cname,course.T) values('002','语文','002'),('003','英语','003');

insert into test.sc (sc.C,sc.score,sc.S) values('001',50,'001'),('001',90,'002'),
                                        ('002',77,'001'),('002',99,'002'),
                                        ('003',100,'001'),('003',33,'002');
insert INTO test.teacher (T,Tname) VALUES('001','刘老师'),('002','王老师'),('003','丧老师');
insert INTO test.teacher (T,Tname) VALUES('004','刘老师')
insert into test.student (student.S,student.Sage,student.Sname,student.Ssex) values('01',12,'小明','男'),('02',12,'小红','女');
insert into test.student (student.S,student.Sage,student.Sname,student.Ssex) values('03',13,'小花','女');
insert into test.student (student.S,student.Sage,student.Sname,student.Ssex) values('04',13,'小绿','女');

查询条件:

//15、删除学习“丧老师”老师课的SC表记录:
delete from test.sc where c=
(select c from test.teacher,test.course where teacher.T=course.T and teacher.Tname='丧老师')
        
//14、查询和“02”号的同学学习的课程完全相同的其他同学学号和姓名:
select s,student.Sname from test.student
        where student.s  in (select s from test.sc where c in (select c from sc where s='02')
        group by s having count(*) =(select count(*) from sc where s='02')) and s !='02'
//13、把“SC”表中“刘老师”老师教的课的成绩都更改为此课程的平均成绩:
update Sc set  score =(
        select a.aa from (
                select avg(sc2.score) aa from sc sc2 ,course where sc2.c=course.c
                       ) a)
        where c in 
                (select c from test.course cs inner join test.teacher th on cs.T=th.T and th.Tname='刘老师')
//12、查询至少学过学号为“01”同学所有一门课的其他同学学号和姓名;
select DISTINCT student.S,student.Sname from test.student,test.sc
        where student.s=sc.S
                and sc.c in(select c from test.course where sc.S='01')
//11、查询至少有一门课与学号为“01”同学所学相同的同学的学号和姓名:
SELECT DISTINCT(student.s),student.Sname from test.student,test.sc,test.course
        where student.s = sc.s
                and sc.C in (select c from sc where sc.s='01')
//10、查询没有学全所有课的同学的学号、姓名:
select student.s,student.Sname from test.student,test.sc
        where sc.s=student.s 
                group by student.s,student.Sname
                        having count(sc.c)<(select count(*) from test.course)
//9、查询所有课程成绩小于60的同学的学号、姓名:
select student.s,student.Sname from test.student
        where student.S not in (select student.s from test.student ,sc where student.s=sc.S and sc.score >60);
//8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名:

//7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名:
select student.s,student.Sname from test.student,test.sc where student.s=sc.s and sc.c='001'and
        EXISTS (select * from sc as sc_2 where sc_2.S=sc.S and sc_2.c='002')
//6、查询学过“刘老师”老师所教的所有课的同学的学号、姓名:
select student.s,student.Sname from test.student 
        where s  in 
                (select sc.s from sc,test.course,test.teacher where sc.c=course.c and teacher.t=course.t and teacher.Tname='刘老师')
//5、查询没有学过“刘老师”老师可的同学的学号、姓名:
select student.s,student.Sname from test.student 
        where s not in 
                (select sc.s from sc,test.course,test.teacher where sc.c=course.c and teacher.t=course.t and teacher.Tname='刘老师')
//4查询姓‘刘’的老师的个数:
select count(distinct(teacher.Tname)) num from test.teacher where teacher.Tname like '刘%'
//1查询“001”课程比“002”课程成绩高的所有学生的学号
select a.s from 
        (SELECT S,score FROM test.sc WHERE sc.C='001') a ,(select S,sc.score from test.sc where sc.C='002')b 
                where a.score>b.score and a.S=b.S
//1.2查询“001”课程比“002”课程成绩高的所有学生的姓名                        
select * from test.student where student.S=
       ( select a.s from 
                (SELECT S,score FROM test.sc WHERE sc.C='001') a ,(select S,sc.score from test.sc where sc.C='002')b 
                        where a.score>b.score and a.S=b.S)
 //2查询平均成绩大于60分的同学的学号和平均成绩
select sc.s, avg(score) from test.sc group by sc.S having avg(Score)>60 
//3、查询所有同学的学号、姓名、选课数、总成绩    
select student.S,student.Sname,count(sc.C),sum(sc.score)from test.student  
        left outer JOIN test.sc on student.S= sc.S   
                GROUP BY student.S,student.Sname                

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/106210.html原文链接:https://javaforall.net

(0)
上一篇 2022年4月23日 下午1:40
下一篇 2022年4月23日 下午2:00


相关推荐

  • J2EE架构师手册

    J2EE架构师手册 

    2022年6月29日
    29
  • SM4算法原理_sgp4算法

    SM4算法原理_sgp4算法前面的文章介绍了SM4算法的C语言实现,源码可见我的另一篇文章:https://blog.csdn.net/cg129054036/article/details/83012721;这篇文章介绍SM4算法原理,这部分可能会比较枯燥,但数学要求也不是太高。目录1.概述2.参数产生3.轮函数4.密钥扩展5.加密/解密过程1.概述2012年3月,国家密码管理…

    2022年10月5日
    5
  • 哪个游戏盒子里有JAVA_1254: 盒子游戏(Java)

    哪个游戏盒子里有JAVA_1254: 盒子游戏(Java)参考博客 Description 有两个相同的盒子 其中一个装了 n 个球 另一个装了一个球 Alice 和 Bob 发明了一个游戏 规则如下 Alice 和 Bob 轮流操作 Alice 先操作 每次操作时 游戏者先看看哪个盒子里的球的数目比较少 然后清空这个盒子 盒子里的球直接扔掉 然后把另一个盒子里的球拿一些到这个盒子中 使得两个盒子都至少有一个球 如果一个游戏者无法进行操作 他 她 就输了 下图是一个典型的游

    2026年3月18日
    2
  • 关于ip地址 :局域网 广域网 ip 公网 私网 ipv6 ipv4的区别、hosts文件和DNS域名解析说明

    关于ip地址 :局域网 广域网 ip 公网 私网 ipv6 ipv4的区别、hosts文件和DNS域名解析说明1.ip地址说明1.1网络基本知识说明(局域网/广域网/ip地址)场景分析:假如只有一台电脑,那么这台电脑的数据只会全部储存在此电脑的硬盘上,与其它的电脑不会产生任何关系,但是一台电脑干的事情比较少,往往需要多台电脑协同办公提高效率。问题1:多台电脑协同工作势必产生数据交互,如何进行数据交互呢???解决1:可以使用u盘之类的移动存储介质拷贝数据(缺点:需要手动的进行操作、无法做到数据的实时传输)解决2:使用线缆把2台电脑连接起来,通过这根线缆进行传输数据,这根线缆就叫做网线。如下图所示。问

    2022年10月18日
    4
  • 【Android TV 开发】–>Leanback 中的 HorizontalGridView

    【Android TV 开发】–>Leanback 中的 HorizontalGridViewLeanBack 是 Google 官方推出的 TV 端的功能库 里面包含了很多在 TVAndroid 端开发常用的控件 本文重点介绍其对 RecyclerView 适配 TV 端做的封装 HorizontalGr

    2026年3月18日
    1
  • web界面设计工具_21个用于Web设计师的生产力工具,可提高他们的设计技能「建议收藏」

    web界面设计工具_21个用于Web设计师的生产力工具,可提高他们的设计技能「建议收藏」web界面设计工具Thegreatthingaboutlivinginourtimeistheadvancementsoftechnology,andwiththatcomestheadvancementsofmoderntechnologyaswell.Therearenumerousprograms,whicharenowavail…

    2022年6月18日
    27

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号