mysql经典查询





建立数据库
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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
———————–数据库建立完成—————————————
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 * from course c1,course c2;
3.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select s.sid,s.sname,avg(score) from student s,sc group by s.sid having avg(score)>60;
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 cname,score from sc,course where sc.cid=course.cid;
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”的课程的同学的信息
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;
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;
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;
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;
mysql经典查询的更多相关文章
- mysql经典查询语句-笔记
笔记来源公开课,谢谢! 1.创建student和score表 CREATE TABLE student ( id INT(10) NOT NULL UNIQUE PRIMARY KEY , name ...
- MySQL经典查询场景
一.设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).用SQL语句创建四个表并完成相关题目. 表结构如下 #建学生信息表 ...
- MySQL必会的28条经典查询
MySQL必会的28条经典查询 原创作品.转载请注明出处https://blog.csdn.net/kk123k 表结构及测试数据请看我上一篇文章:学生选修成绩表测试数据 Student(Sno, ...
- mysql连接查询经典小例题
mysql连接查询: Mysql连接查询支持多表连接 对同一张表可以重复连接多次(别名在多次连接同一张表时很重要) 例题1: 下面有2张表 teams表 比赛结果表:result 问题: 得出一张表: ...
- 10 个 MySQL 经典错误【转】
Top 1:Too many connections(连接数过多,导致连接不上数据库,业务无法正常进行) 问题还原 mysql> show variables like '%max_connec ...
- 数据库MySQL经典面试题之SQL语句
数据库MySQL经典面试题之SQL语句 1.需要数据库表1.学生表Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学 ...
- 使用聚集索引和非聚集索引对MySQL分页查询的优化
内容摘录来源:MSSQL123 ,lujun9972.github.io/blog/2018/03/13/如何编写bash-completion-script/ 一.先公布下结论: 1.如果分页排序字 ...
- 【不断更新】mysql经典50道题自我练习
mysql经典50道题自我练习 测试数据和练习题均转载自CSDN博主@启明星的指引的文章sql语句练习50题(Mysql版),用于mysql的每日自我练习 表名和字段 –1.学生表 Student(s ...
- MYSQL经典练习题,熟悉DQL
MYSQL经典练习题 (本练习题可让你熟悉DQL,快速的上手DQL) 首先,先在数据库中建立基本数据库以及表项: DROP DATABASE IF EXISTS `test`; CREATE DATA ...
随机推荐
- Hibernate 之核心接口
1.持久化和ORM 持久化是指把数据(内存中的对象)保存到可持久保存的存储设备中(如硬盘),主要应用于将内存中的数据存储到关系型数据库中,在三层结构中,持久层专注于实现系统的逻辑层面,将数据使用者与数 ...
- Python2.4+ 与 Python3.0+ 主要变化与新增内容
Python2 Python3print是内置命令 print变为函数print >> f,x,y ...
- 开发者常用的十款Chrome插件
本文是稀土掘金投稿,虽然其中有倔金的私货,是篇推广文,但我看过后认为内容确实不错,有些好插件还是第一次知道,对我很有帮助,考虑过后还是决定推荐给大家,最近我比较关注各种提高开发效率的工具与技巧,今后看 ...
- 赛肯德 | 2017NEERC某题
我们枚举每一条边的流量x,将它作为底流(也就是比它大的的流量变成差值,比它小的流量为0),然后我们设x就是路径上第K大的那个边的流量.然后跑最短路,加上dis[n]就是当前的答案.然后取min即可. ...
- GitHub创建项目,保存代码。
平时学习会写一些代码,虽然只是零零散散的功能,但是基本都是在一个项目下操作,偶尔会忘记代码编辑顺序.国庆这几天在家,想把GitHub用起来,实现自己代码的可追溯,可查询.学习本篇博客,你需要一点的Gi ...
- python 开发工具IDE pycharm的破解版安装
打开终端 cd /etc 命令行输入 sudo vim hosts 输入mac密码 输入i,进入编辑模式(注意在英文状态下书写) 粘贴0.0.0.0 account.jetbrains.com到文件最 ...
- BootStrap Modal 点击空白时自动关闭
本文为大家讲解的是如何禁用 BootStrap Modal 点击空白时自动关闭的方法,感兴趣的同学参考下. 方法如下 $('#myModal').modal({backdrop: 'static', ...
- [整理]Unicode 与 UTF8
目录 先上总结 ASCII utf-8 编码规则 UTF-16 其他 先上总结 Unicode 是一个符号集, 规定了所有符号的二进制编号. UTF8 是unicode的一种编码方式(存储, 传输方式 ...
- java四行代码实现图片下载
如下: InputStream in = new URL("http://www.updown/thumbnail.jpg).openStream(); Path temp = Paths. ...
- P1091 合唱队列
合唱队列 原题:传送门 核心代码: /* 方法求出每一个点的最长升子序列和最长降子序列,再加到该点上 通过循环比较哪个点最大,再用总长减去该点长度即是答案 */ #include<iostrea ...