建立数据库

1.建立一个数据库 
create database work;

2.进入数据库work 
use work;

3.数据库默认编码可能不支持中文,可以在这里设置下 
set names gbk;

4.建立student表 
属性有:编号:id (主键,自动增长),姓名:sname,出生年月:sage,性别:ssex(枚举) 
create table student(sid int primary key auto_increment, 
sname varchar(20), 
sage date, 
ssex enum(‘男’,’女’));

5.第二个课程表中使用了外键教师标号,因而需要先建立教师表 
create table teacher(tid int primary key auto_increment, 
tname varchar(20));

6.建立课程表 
create table course(cid int primary key auto_increment, 
cname varchar(20), 
tid int, 
foreign key(tid) references teacher(tid));

7.建立成绩表 
create table sc(sid int, 
cid int, 
score int);

8.show tables; //可查看建立的四个表格

9.插入数据,因为里面有主键链接,表格插入数据也要有顺序(注意题目图片上都是字节引号,应该为int,不要单引号)

a,先给student表插入数据
insert into student values(1,'赵雷','1990-01-01','男'),(2,'钱电','1990-12-21','男'),(3,'孙风','1990-05-20','男'),(4,'李云','1990-08-06','男'),(5,'周梅','1991-12-01','女'),(6,'吴兰','1992-03-01','女'),(7,'郑竹','1989-07-01','女'),(8,'王菊','1990-01-20','女');b, 给teacher表插入数据,这里不可以先给course表插入数据,因为course表外链接到teacher的主键
insert into teacher values(1,'张三'),(2,'李四'),(3,'王五');c, 给course表插入数据
insert into course values(1,'语文',2),(2,'数学',1),(3,'英语',3);d, 最后给sc表插入数据(题目图片少了第1个学生成绩,在这加上 1,1,90;  1,2,80;  1,3,90)
insert into sc values(1,1,90),(1,2,80),(1,3,90),(2,1,70),(2,2,60),(2,3,80),(3,1,80),(3,2,80),(3,3,80),(4,1,50),(4,2,30),(4,3,20),(5,1,76),(5,2,87),(6,1,31),(6,3,34),(7,2,89),(7,3,98);

———————–数据库建立完成—————————————


1、查询”01”课程比”02”课程成绩高的学生的信息及课程分数 
select s.sid,s.sname,s.sage,s.ssex,sc1.score,sc2.score from student s,sc sc1,sc sc2 where sc1.cid=1 and sc2.cid=2 and sc1.score>sc2.score and sc1.sid=sc2.sid and s.sid=sc1.sid;

2、查询同时存在”01”课程和”02”课程的学生情况

SELECT a.*, b.score, c.score FROM student a, sc b,sc c  where 1 =1
and a.sid = b.sid and a.sid = c.sid and b.cid = 1 and c.cid = 2

3.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩 

select t.sid,t.avgnum, (select sname from student where sid = t.sid) as sname
from (select sid, sum(score)/3 as avgnum from sc group by sid having sum(score)/3 >= 60) as t

4、查询名字中含有”风”字的学生信息 
select * from student where sname like '%风%';

5、查询课程名称为”数学”,且分数低于60的学生姓名和分数 
select s.sname,score from student s,sc where s.sid=sc.sid and cid=2 and score<60;

6、查询所有学生的课程及分数情况;

SELECT s.sid, s.sname, c.cname,sc1.score
FROM sc sc1,course c,student s
WHERE sc1.cid=c.cid AND s.sid = sc1.sid;

7、查询没学过”张三”老师授课的同学的信息 
select s.* from student s where s.sid not in(select sc1.sid from sc sc1,course c,teacher t where t.tid=c.tid and sc1.cid=c.cid and t.tname=’张三’);

8.查询学过”张三”老师授课的同学的信息 
select s.* from student s ,sc sc1,course c,teacher t where s.sid=sc1.sid and sc1.cid=c.cid and c.tid=t.tid and t.tname=‘张三’;

9、查询学过编号为”01”并且也学过编号为”02”的课程的同学的信息 
select s.* from student s,sc sc1,sc sc2 where s.sid=sc1.sid and sc1.sid=sc2.sid and sc1.cid=1 and sc2.cid=2;

10、查询学过编号为”01”但是没有学过编号为”02”的课程的同学的信息 
select distinct s.* from student s,sc sc1,sc sc2,sc sc3 where s.sid=sc1.sid and sc1.sid=sc2.sid and sc1.cid=1 and sc2.cid!=2;

select t1.*
from student t1
where t1.sid not in (select sid from sc where cid =2)
and t1.sid in (select sid from sc where cid =1)

11、查询没有学全所有课程的同学的信息 
select s.* from student s where s.sid not in(select sc1.sid from sc sc1,sc sc2,sc sc3 where sc1.cid=1 and sc2.cid=2 and sc3.cid =3 and sc1.sid=sc2.sid and sc1.sid=sc3.sid) group by s.sid;

12、查询至少有一门课与学号为”01”的同学所学相同的同学的信息 
select distinct s.* from student s,sc sc1 where s.sid=sc1.sid and sc1.cid in(select cid from sc where sid=1) and s.sid<> 1;

13、查询和”01”号的同学学习的课程完全相同的其他同学的信息 
select s.* from student s where s.sid in(select distinct sc.sid from sc where sid<>1 and sc.cid in(select distinct cid from sc where sid=1)group by sc.sid having count(1)=(select count(1) from sc where s.sid=1));

14、查询没学过”张三”老师讲授的任一门课程的学生姓名 
select s.* from student s where s.sid not in(select sc1.sid from sc sc1,course c,teacher t where sc1.cid=c.cid and c.tid=t.tid and t.tname=’张三’);

15、查询出只有两门课程的全部学生的学号和姓名 
select s.* from student s,sc group by sc.sid having count(sc.sid)=2 and s.sid=sc.sid;

select t.sid,(select sname from student where sid = t.sid) as name,t.count from
(select sid, count(sid) as count from sc group by sid  having count(sid) = 2) as t;

16、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime) 
select * from student where sage>=’1900-01-01’ and sage<=’1900-12-31’; 
select s.* from student s where s.sage like ‘1900-%’;(方法2)

17、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 
select sc.cid,avg(score) from sc group by sc.cid order by avg(score) DESC , sc.cid;

18、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; 
select s.sname,c.cname,score from student s,sc,course c where s.sid=sc.sid and sc.cid=c.cid and score>70;

19、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 
select s.sname,avg(score) from sc,student s where s.sid=sc.sid group by sc.sid having avg(score)>=85;

20、查询不及格的课程 
select s.sname,c.cname,score from student s,sc,course c where s.sid=sc.sid and sc.cid=c.cid and score<60;

21、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名; 
select s.sid,s.sname from student s,sc where sc.sid=s.sid and sc.cid=1 and score>80;

22、求每门课程的学生人数 
select cid,count(sid) from sc group by sc.cid;

23、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 
select cid,count(sid) from sc group by cid having count(sid)>5 order by count(sid),cid ASC;

24、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 
select s1.sid,s2.sid,sc1.cid,sc1.score,sc2.score from student s1,student s2,sc sc1,sc sc2 where s1.sid!=s2.sid and s1.sid=sc1.sid and s2.sid=sc2.sid and sc1.cid!=sc2.cid and sc1.score=sc2.score;

25、检索至少选修两门课程的学生学号 
select sid from sc group by sid having count(cid)>=2;

26、查询选修了全部课程的学生信息 

select s.* from sc,student s where s.sid=sc.sid group by sid having count(cid)=3;

27、查询各学生的年龄 
select s.sname,(TO_DAYS(‘2017-09-07’)-TO_DAYS(s.sage))/365 as age from student s;

28、查询本月过生日的学生 
select s.sname from student s where s.sage like ‘_____07%’;

29、查询下月过生日的学生 
select s.sname from student s where s.sage like ‘_____08%’;

30、查询学全所有课程的同学的信息 
select s.* from student s,sc sc1,sc sc2,sc sc3 where sc1.cid=1 and sc2.cid=2 and sc3.cid=3 and sc1.sid=sc2.sid and sc1.sid=sc3.cid and s.sid =sc1.sid group by s.sid;

(教师,学生,成绩,课程表)关联表查询相关推荐

  1. mongodb关联表查询

    1.首先自定义一个查询的Operation package com.pica.wx.bean;import com.mongodb.DBObject; import org.springframewo ...

  2. Mybatis源码分析--关联表查询及延迟加载原理(二)

    在上一篇博客Mybatis源码分析--关联表查询及延迟加载(一)中我们简单介绍了Mybatis的延迟加载的编程,接下来我们通过分析源码来分析一下Mybatis延迟加载的实现原理. 其实简单来说Myba ...

  3. Mybatis实现关联表查询

    Mybatis实现关联表查询 6.1. 一对一关联 1). 提出需求 根据班级 id 查询班级信息(带老师的信息) 2). 创建表和数据 [](javascript:void(0)

  4. MySQL 实现多张无关联表查询数据并分页

    MySQL 实现多张无关联表查询数据并分页 1.功能需求 在三张没有主外键关联的表中取出自己想要的数据,并且分页. 2.数据库表结构 水果表: 坚果表: 饮料表: 主要用UNION AL UNION ...

  5. hibernate mysql 关联查询_hibernate 查询缓存,以及在关联表查询缓存下使用HQL,而不是sql去查询...

    什么是查询缓存? 也就是如果开启了查询缓存, 在 没有使用二级缓存的情况下,会 缓存第一次查询出来的数据的id. 第二次查询的时候, 如果查询的参数和查询语句没有变化,那么就会使用 第一次查询的出来的 ...

  6. MySQL经典四表查询(教师,学生,成绩,课程表)多表查询

    我用的工具:MySQL5.7+SQLyog-11.2.5-0 1.表架构呈现 student(sid,sname,sage,ssex) 学生表 teacher(tid,tname) 教师表 cours ...

  7. mysql查询未讲课教师_经典教师 学生 成绩sql面试题再次来袭3(附答案)

    概述 续一下之前讲的MySQL数据库sql基础练习第三部分,大家可以跟着做一下. 建表语句CREATE TABLE students (sno VARCHAR(3) NOT NULL, sname V ...

  8. MyBatis学习总结(五)——实现关联表查询

    2019独角兽企业重金招聘Python工程师标准>>> 一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里 ...

  9. MyBatis学习总结(5)——实现关联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

最新文章

  1. centos 7 安装GTK+-2.0
  2. Bitmap 索引 vs. B-tree 索引:如何选择以及何时使用?——4-5
  3. Maven plugins和pluginManagement的区别概述
  4. linux下安装nginx1.10,Linux(RHEL7.0)下安装nginx-1.10.2(示例代码)
  5. java程序 启动慢_spring boot 程序启动缓慢的问题
  6. mysql8.0导入备份_mysql8.0.20配合binlog2sql的配置和简单备份恢复的步骤详解
  7. PHP中 htmlspecialchars,htmlentities, nl2br函数
  8. mysql搜索中文 有的匹配不出来_MYSQL-中文检索匹配与正则表达式
  9. java easyui分页源码_SpringMVC+easyUI中datagrid分页实现_2014.5.1
  10. php静态方法的问题,php 静态方法问题
  11. drools规则引擎通过数据库存储规则
  12. 下载firebug网站
  13. 车载中控桌面布局android,安卓车载导航桌面主题
  14. 2020年6月TIOBE编程语言排名公布
  15. IE浏览器代理出问题导致的程序网络不可用
  16. 10.3、DHCPv6原理与配置
  17. mac发送微信表情卡顿(已解决!!!!)
  18. 1000张动漫萌妹子次元系人物素材图,可做壁纸头像
  19. iOS CocoaPods1.0.0的一些基础知识
  20. 关于差旅费的账务处理

热门文章

  1. TIA博途wincc V16 如何进行变量周期归档?
  2. TIA博途SCL编程学习22_奖金计算
  3. 从英语转行计算机,太不容易了!为了高薪,值!
  4. 麦当劳中国再生塑料变身新餐盘;VEDETT罐装白熊玫瑰红啤酒全新上市 | 知消
  5. 《足球弹弹乐》成爆款获腾讯精品推荐,Cocos 专访开发商解密成功之道
  6. JavaScript数组去重方法整理
  7. oracle“靠”字的困惑
  8. python更换窗口颜色设置_python – 默认窗口颜色Tkinter和十六进制颜色代码
  9. file上传代码 ios_iOS视频压缩存储至本地并上传至服务器实例代码
  10. linux sockaddr in,Linux下获取sockaddr的方法