创建数据库

1.创建一个数据库

create database work;web

2.进入数据库work

use work;数据库

3.数据库默认编码可能不支持中文,能够在这里设置下

set names gbk;svg

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));code

7.创建成绩表

create table sc(sid int,

cid int,

score int);xml

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

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);

-----------------------数据库创建完成---------------------------------------ci

一、查询"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;

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

select s.sid,s.sname,avg(sc.score) from student s,sc group by s.sid having avg(sc.score)>=60;

四、查询名字中含有"风"字的学生信息

select * from student where sname like ‘%风%’;

五、查询课程名称为"数学",且分数低于60的学生姓名和分数

select s.sname,score from student s,sc where s.sid=sc.sid and cid=2 and score<60;

六、查询全部学生的课程及分数状况;

select cname,score from sc,course where sc.cid=course.cid;

七、查询没学过"张三"老师授课的同窗的信息

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=‘张三’;

九、查询学过编号为"01"而且也学过编号为"02"的课程的同窗的信息

student(sid) sc(sid cid tid) sc2(sid cid tid) course(cid tid cname)

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;

十、查询学过编号为"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 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;

十二、查询至少有一门课与学号为"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;

1三、查询和"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));

1四、查询没学过"张三"老师讲授的任一门课程的学生姓名

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=‘张三’);

1五、查询出只有两门课程的所有学生的学号和姓名

select s.* from student s,sc group by sc.sid having count(sc.sid)=2 and s.sid=sc.sid;

1六、查询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)

1七、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select sc.cid,avg(score) from sc group by sc.cid order by avg(score) DESC , sc.cid;

1八、查询任何一门课程成绩在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;

1九、查询平均成绩大于等于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;

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

select s.sid,s.sname from student s,sc where sc.sid=s.sid and sc.cid=1 and score>80;

2二、求每门课程的学生人数

select cid,count(sid) from sc group by sc.cid;

2三、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select cid,count(sid) from sc group by cid having count(sid)>5 order by count(sid),cid ASC;

2四、查询不一样课程成绩相同的学生的学生编号、课程编号、学生成绩

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;

2五、检索至少选修两门课程的学生学号

select sid from sc group by sid having count(cid)>=2;

2六、查询选修了所有课程的学生信息

select s.* from sc,student s where s.sid=sc.sid group by sid having count

(cid)=3;

2七、查询各学生的年龄

select s.sname,(TO_DAYS(‘2017-09-07’)-TO_DAYS(s.sage))/365 as age from student s;

2八、查询本月过生日的学生

select s.sname from student s where s.sage like ‘_____07%’;

2九、查询下月过生日的学生

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;

mysql经典四表查询_mysql经典问题四表查询(教师,学生,成绩,课程表)相关推荐

  1. mysql的四表查询_Mysql经典四表查询

    **建立student表** 属性有:编号:id (主键,自动增长),姓名:sname,出生年月:sage,性别:ssex(枚举) create table student(sid int prima ...

  2. mysql面试题学校三表查询_mysql经典面试题之学生成绩表

    需要数据库表1.学生表 Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 2.课程表 Course(CID, ...

  3. mysql中select 的题型_MYSQL经典题型详情解析

    学完了mysql后发现有很多地方不是很明白,于是总结了mysql的经典题型,不论是工作还是面试,我相信还是有一定帮助的. 例题一 ​ 在我的数据库中数据如下(排序有些差别,但是不影响结果) ​ 分析: ...

  4. 如何避免mysql回表查询_mysql如何避免回表查询

    <迅猛定位低效SQL?>留了一个尾巴: select id,name where name='shenjian' select id,name,sexwhere name='shenjia ...

  5. mysql 左关联查询_MySQL 左关联右表条件查询

    MySQL的新知识: 之前数据库没学好,不知道LEFT JOIN ON后边可以加条件 首先业务是有个A表,然后会依据A表信息来在B表生成一条对应数据, 操作时候展示A表数据,如果B表已经有对应数据,显 ...

  6. MySQL实验四数据库的查询_MySQL数据库实验四:嵌套查询

    实验四          嵌套查询 一.实验目的 掌握SELECT语句的嵌套使用,实现表的复杂查询,进一步理解SELECT语句的高级使用方法. 二.实验环境 三.实验示例 1.  查询与"刘 ...

  7. mysql 回表查询优化_MySQL中的回表查询与索引覆盖:一次百万级别分页查询使用Limit 从90秒到0.6毫秒的优化...

    这里写目录标题 事故现场 解决方案 提到的"回表查询" InnoDB的索引 什么是回表查询 怎么优化回表查询 事故现场 数据库使用的MySQL,有一个日志表,需要进行分页查询,于是 ...

  8. mysql同张表关联多次查询_MySQL多次单表查询和多表联合查询

    Tip:不建议执行三张表以上的多表联合查询 对数据量不大的应用来说,多表联合查询开发高效,但是多表联合查询在表数据量大,并且没有索引的时候,如果进行笛卡儿积,那数据量会非常大,sql执行效率会非常低 ...

  9. mysql 多个select查询_MySQL的select多表查询

    select 语句: select 语句一般用法为: select 字段名 from tb_name where 条件 ; select 查询语句类型一般分为三种: 单表查询,多表查询,子查询 最简单 ...

最新文章

  1. ologit模型与logit_Stata-多元 Logit 模型详解 (mlogit)
  2. C#.NET 轻量级通用快速开发平台,DevExpress DXperience 12.2
  3. Spring Boot处理静态资源(自定义资源映射)
  4. WinCVS与CVSNT简明使用手则
  5. QML中类似QMap的用法
  6. 读完这10本书,“大数据”对你来说,或许就是小菜一碟
  7. 量化评估推荐系统效果
  8. iOS 7.1下itms-services在线安装失败的解决方法
  9. java 放行文件_springmvc中关于静态资源的放行
  10. 万字长文 | 线性代数的本质课程笔记完整合集!
  11. Android系统级深入开发——移植与调试
  12. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
  13. 100套精品PPT模板免费拿!以后再也不用怕老板叫你制作PPT了
  14. 计算机控制技术课程2018更新资料
  15. React-Native调用系统分享组件Share组件的使用
  16. 【GNN报告】微软亚洲研究院郑书新:图神经网络迈入Transformer时代
  17. 【附源码】计算机毕业设计SSM企业合同管理系统
  18. 博客专家 - 2022贡献榜与TOP100光荣榜【转】
  19. 机器学习 梯度到底是什么?
  20. Linux——详解进程控制之终止

热门文章

  1. 利用Sigar获取系统信息
  2. AW_blog 在任意页设置文章的数量
  3. Windows Vista桌面窗口管理器(3)
  4. python将一组数据转化为列表_Pandas将列表(List)转换为数据框(Dataframe)
  5. Python生成CSV文件模拟某小区用户手机通话记录
  6. Linux 文件系统 软/硬链接文件
  7. JDBC中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
  8. 二元函数求最小值 c语言,遗传算法C语言源代码(一元函数和二元函数)
  9. python选股源代码_【一点资讯】Python实现行业轮动量化选股【附完整源码】 www.yidianzixun.com...
  10. 代替紧急通知_人员紧急替代程序与替代方案的通知