From 55440fe669317b95487faa1cc68c3c5efe14fa5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=96=8C?= <280180014@qq.com> Date: Wed, 8 Mar 2023 00:19:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=90=B4=E6=96=8C=E7=9A=84=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...6 \345\255\220\346\237\245\350\257\242.md" | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 "15 \345\220\264\346\226\214/20230306 \345\255\220\346\237\245\350\257\242.md" diff --git "a/15 \345\220\264\346\226\214/20230306 \345\255\220\346\237\245\350\257\242.md" "b/15 \345\220\264\346\226\214/20230306 \345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..0d6d60f --- /dev/null +++ "b/15 \345\220\264\346\226\214/20230306 \345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,104 @@ +# 笔记 + +| 操作符 | 描述 | +| ------ | -------------------------------------- | +| IN | 在指定的集合范围内,多选一 | +| NOT IN | 不在指定的集合范围内 | +| ANY | 子查询返回列表中,有任意一个满足即可 | +| SOME | 与ANY等同,使用SOME的地方都可以使用ANY | +| ALL | 子查询返回列表的所有值都必须满足 | + +# 作业 + +```mysql +create database class1 charset utf8; +use class1; +create table stuinfo( + stuNO varchar(5) primary key, + stuName varchar(4), + stuSex enum('男','女'), + stuAge int, + stuAddress varchar(4), + stuSeat int +); + +create table stuExam( + examNO int primary key auto_increment, + stuNO varchar(5) , + writtenExam int, + labExam int, + foreign key (stuNO) references stuinfo(stuNO) +); + +create table stuMarks( + examNO int, + stuID varchar(5), + score int, + foreign key (examNO) references stuExam(examNO) +); + +insert into stuinfo values ('s2501','张秋利','男',20,'美国硅谷',1), + ('s2502','李斯文','女',18,'湖北武汉',2), + ('s2503','马文才','男',18,'湖南长沙',3), + ('s2504','欧阳俊雄','女',21,'湖北武汉',4), + ('s2505','梅超风','男',20,'湖北武汉',5), + ('s2506','陈旋风','男',19,'美国硅谷',6); + +insert into stuExam values (1,'s2501',50,70),(2,'s2502',60,65), + (3,'s2503',86,70),(4,'s2504',40,80), + (5,'s2505',70,85),(6,'s2506',85,90); + +insert into stuMarks values (1,'s2501',88),(2,'s2501',92),(3,'s2501',53), + (4,'s2502',60),(5,'s2502',99),(6,'s2503',83); + +# 1.查询出年龄比班上平均年龄大的学生的信息 +select * +from stuinfo where stuAge>(select avg(stuAge) from stuinfo); + +# 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select stuinfo.stuNO,stuName,stuSex,ifnull(max(score),0) +from stuinfo left join stuMarks on stuinfo.stuNO =stuMarks.stuID group by stuNO; + +# 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select stuinfo.stuNO,stuName,(writtenExam+stuExam.labExam)/2 as 平均分 +from stuinfo,stuExam where stuExam.stuNO=stuinfo.stuNO; + +# 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuSex='男' and stuAge>=20; +select * from stuinfo where stuSex='男' and stuAge in(select stuAge from stuinfo where stuAge>=20 and stuSex='男'); + +# 5.查询出年龄比所有男生年龄都大的女生的信息 +select * +from stuinfo where stuSex='女' and stuAge>(select max(stuAge) from stuinfo where stuSex='男'); + +# 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select stuinfo.* from stuinfo,stuMarks where stuinfo.stuNO=stuMarks.stuID group by stuID having min(score)>=60; + +# 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select distinct stuinfo.* from stuinfo,stumarks where stuNO=stuID; +select * from stuinfo where stuNO in (select stuid from stumarks); + +# 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select stuinfo.* from stumarks right join stuinfo on stumarks.stuid=stuinfo.stuno where score is null; +select * from stuinfo where stuNO not in (select stuid from stumarks); + +# 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select * +from stuinfo,stumarks where stuNO=stuID and score>90; + +# 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select stuinfo.* from stuinfo,stumarks where stuNO=stuID group by stuNO having avg(score)>80; + +# 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select stuinfo.* from stuMarks right join stuinfo on stuinfo.stuNo=stuMarks.stuid where score>(select max(score) from stumarks where stuid='s2501'); + +# 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select stuinfo.* from stuinfo left join stuExam on stuinfo.stuNO = stuExam.stuNO +where writtenExam>(select writtenExam from stuExam where stuNo=(select stuNo from stuinfo where stuname='张秋利')) OR labExam>(select labExam from stuExam where stuNo=(select stuNo from stuinfo where stuname='张秋利')); + +# 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stuinfo where stuAge>(select max(stuAge) from stuinfo where stuSex='男') and stuSex='女'; + +# 14.查询出只是比某个男生年龄大的女生的信息 +select * from stuinfo where stuAge>any(select stuAge from stuinfo where stuSex='男') and stusex='女'; +``` \ No newline at end of file -- Gitee From d9f63f7b5f1198c9449480e23743b33f8e922eec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=96=8C?= <280180014@qq.com> Date: Wed, 8 Mar 2023 23:40:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=90=B4=E6=96=8C=E7=9A=84=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...8 \345\244\247\344\275\234\344\270\232.md" | 217 ++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 "15 \345\220\264\346\226\214/20230308 \345\244\247\344\275\234\344\270\232.md" diff --git "a/15 \345\220\264\346\226\214/20230308 \345\244\247\344\275\234\344\270\232.md" "b/15 \345\220\264\346\226\214/20230308 \345\244\247\344\275\234\344\270\232.md" new file mode 100644 index 0000000..0c252a0 --- /dev/null +++ "b/15 \345\220\264\346\226\214/20230308 \345\244\247\344\275\234\344\270\232.md" @@ -0,0 +1,217 @@ +# 作业 + +```mysql +# # MySQL基础结课考试 +# +# ## 考试时间 120 分钟 总分:100分 +# +# **场景**: +# +# 你在一个软件公司上班,今天公司接一个新业务。要用MySQL给一个小说网站设计一个数据库。 +# +# **数据库名**:xiaoshuo +# +# 该数据库里有四张表:作家信息表 ( author )、作家等级信息表 ( vip )、小说作品信息表 ( story )、小说作品类型表 ( type ) +# +# ### 1、相关表结构 +create database xiaoshuo charset utf8; +use xiaoshuo; + +# 1. 作家信息表 ( author ) (4分) +# +# | 字段名称 | 数据类型 | 说明及要求 | +# | ----------- | ----------- | --------------------------------- | +# | author_id | int | 作家编号,主键 | +# | author_name | varchar(20) | 作家姓名、非空、不能重复 | +# | credits | int | 积分 | +# | vip_id | varchar(20) | 等级编号,非空、外键关联等级信息表 | +create table author( + author_id int primary key comment '作家编号', + author_name varchar(20) not null unique comment '作家姓名', + credits int comment '积分', + vip_id varchar(20) not null comment '等级编号', + foreign key (vip_id) references vip(vip_id) +); + + +# 2. 作家等级信息表 ( vip ) (3分) +# +# | 字段名称 | 数据类型 | 说明及要求 | +# | -------- | ----------- | ------------------------ | +# | vip_id | varchar(20) | 等级编号,主键 | +# | vip_name | varchar(20) | 等级名称,非空,不能重复 | +create table vip( + vip_id varchar(20) primary key comment '等级编号', + vip_name varchar(20) not null unique comment '等级名称' +); + + +# 3. 小说作品信息表 ( story )(4分) +# +# | 字段名称 | 数据类型 | 说明及要求 | +# | ------------ | ----------- | ----------------------------- | +# | story_id | int | 作品编号,主键,自增 | +# | author_id | int | 作家编号,外键,关联作家信息表 | +# | type_id | varchar(20) | 类型编号,外键,关键作品类型表 | +# | story_name | varchar(50) | 作品名称 | +# | views_number | int | 浏览量 | +create table story( + story_id int primary key auto_increment comment '作品编号', + author_id int comment '作家编号', + type_id varchar(20) comment '类型编号', + story_name varchar(50) comment '作品名称', + views_number int comment '浏览量', + foreign key (author_id) references author(author_id), + foreign key (type_id) references type(type_id) +); + +# 4. 小说作品类型表 ( type )(2分) +# +# +# | 字段名称 | 数据类型 | 说明及要求 | +# | --------- | ----------- | ------------------------ | +# | type_id | varchar(20) | 类型编号,主键 | +# | type_name | varchar(20) | 类型名称,非空,不能重复 | +create table type( + type_id varchar(20) primary key comment '类型编号', + type_name varchar(20) not null unique comment '类型名称' +); + + +# ### 2、对应的表数据 +# +# 1. 作家信息表 (4分) +# +# | 作家编号 | 作家名称 | 积分 | 等级编号 | +# | :------: | :------: | :--: | :------: | +# | 1001 | 朱逸群 | 600 | VIP01 | +# | 1002 | 范建 | 8510 | VIP04 | +# | 1003 | 史珍香 | 981 | VIP02 | +# | 1004 | 范统 | 2364 | VIP02 | +# | 1005 | 杜子腾 | 257 | VIP01 | +# | 1006 | 刘产 | 678 | VIP02 | +# | 1007 | 杜琦燕 | 438 | VIP03 | +insert into author values (1001,'朱逸群',600,'VIP01'),(1002,'范建',8510,'VIP04'), + (1003,'史珍香',981,'VIP02'),(1004,'范统',2364,'VIP02'), + (1005,'杜子腾',257,'VIP01'),(1006,'刘产',678,'VIP02'), + (1007,'杜琦燕',438,'VIP03'); + + +# 2. 等级信息表(2分) +# +# | 等级编号 | 等级名称 | +# | :------: | :------: | +# | VIP01 | 青铜作家 | +# | VIP02 | 白银作家 | +# | VIP03 | 黄金作家 | +# | VIP04 | 钻石作家 | +insert into vip values ('VIP01','青铜作家'),('VIP02','白银作家'), + ('VIP03','黄金作家'),('VIP04','钻石作家'); + + +# 3. 小说作品信息表(5分) +# +# | 作品编号 | 作家编号 | 类型编号 | 作品名称 | 订阅数 | +# | :------: | :------: | :------: | :----------------------: | :----: | +# | 1 | 1002 | L03 | 母猪产后与护理师的二三事 | 6541 | +# | 2 | 1005 | L04 | 拖拉机大战蜘蛛侠 | 563 | +# | 3 | 1003 | L01 | 这只小龙虾不正经 | 8754 | +# | 4 | 1006 | L04 | 一个爹爹三个娃 | 36354 | +# | 5 | 1006 | L01 | 皇上滚开本宫只劫财 | 3674 | +# | 6 | 1005 | L05 | 给长城贴瓷砖的小太监 | 6541 | +# | 7 | 1003 | L03 | 不科学御兽 | 1257 | +# | 8 | 1005 | L01 | 镜面管理局 | 3216 | +# | 9 | 1004 | L02 | 关于我成为灭魂师之后 | 1147 | +# | 10 | 1004 | L05 | 公子别秀 | 2078 | +insert into story values (1,1002,'L03','母猪产后与护理师的二三事',6541),(2,1005,'L04','拖拉机大战蜘蛛侠',563), + (3,1003,'L01','这只小龙虾不正经',8754),(4,1006,'L04','一个爹爹三个娃 ',36354), + (5,1006,'L01','皇上滚开本宫只劫财',3674),(6,1005,'L05','给长城贴瓷砖的小太监',6541), + (7,1003,'L03','不科学御兽',1257),(8,1005,'L01','镜面管理局',3216), + (9,1004,'L02','关于我成为灭魂师之后',1147),(10,1004,'L05','公子别秀 ',2078); + +# 4. 作品类型(3分) +# +# | 类型编号 | 类型名称 | +# | :------: | :------: | +# | L01 | 玄幻 | +# | L02 | 奇幻 | +# | L03 | 武侠 | +# | L04 | 仙侠 | +# | L05 | 都市 | +insert into type values ('L01','玄幻'),('L02','奇幻'), + ('L03','武侠'),('L04','仙侠'), + ('L05','都市'); + + +# +# ### 3、题目 +# +# > 所有题目要求使用SQL语句完成 +# +# 1. 根据前面提供的表结构和表数据,创建数据库并分别创建这张四张表;并插入相关数据。(提醒:外键请注意建表顺序和插入数据的顺序) (30分) +# 2. 将story 表中的story_name字段类型改成varchar(40) 。(2分) +alter table story modify story_name varchar(40); + +# 3. 在author表中增加一个性别字段 字段名:author_sex,类型: char(10),要求默认值为'男'。 (3分) +alter table author add author_sex char(10) default '男'; + +# 4. 将作家编号为1005、1007的作家性别改为'女' 。(2分) +update author set author_sex='女' where author_id in (1005 ,1007); + +# 5. 作家杜子腾,写了一篇名为《拜登夸我很帅》的都市小说,有854个浏览量,请将这条信息插入到story表。(3分) +insert into story value (11,1005,'L05','拜登夸我很帅',854); + +# 6. 《拖拉机大战蜘蛛侠》这篇小说,浏览量涨了100,请更新story表中的相关数据。(2分) +update story set views_number = views_number+100 where story_name='拖拉机大战蜘蛛侠'; + +# 7. 请删除story表的中《皇上滚开本宫只劫财》这篇小说相关数据。(2分) +delete from story where story_name='皇上滚开本宫只劫财'; + +# 8. 查询 浏览量大于 8000的小说的作者编号和小说作品名称。(2分) +select story_id,story_name +from story where views_number>8000; + +# 9. 查询积分大于1000 并且会员等级高于vip03的作家所有信息。(3分) +select * +from author where credits>1000 and vip_id>'vip03'; + +# 10. 查询姓名以杜字开头的作家的姓名,积分和等级编号。(3分) +select author_name,credits,vip_id +from author where author_name like '杜%'; + +# 11. 查询积分在100、1000之间的作家信息,以积分降序排列。 (3分) +select * from author where credits between 100 and 1000 order by credits desc ; + +# 12. 查询出小说的总浏览量,最高浏览量,最小浏览量,平均浏览量,给字段用上中文别名。(3分) +select sum(views_number) 总浏览量,max(views_number) 最高浏览量,min(views_number) 最小浏览量,avg(views_number) 平均浏览量 +from story; + +# 13. 查询各种等级的作家的平均积分和作家数量,并对查询结果使用中文别名。(3分) +select avg(credits) 平均积分,count(vip_id) 玩家数量 +from author group by vip_id; + +# 14. 查询小说数量大于等于2的分类编号和小说数量。(4分) +select type_id,count(type_id) +from story group by type_id having count(type_id)>=2; + +# 15. 查询所有小说中浏览量最少的书的作品编号、作品名称和类型编号、浏览量。(4分) +select author_id,type_id, story_name, views_number +from story where views_number=(select min(views_number)from story); + +# 16. 查询积分比刘产高的作者所有信息。(5分) +select * +from author where credits>(select credits from author where author_name='刘产'); + +# 17. 查询出哪些白银作家是没有写小说的,显示这些作家的姓名、等级名称。(8分) +select * +from author left join story on author.author_id = story.author_id join vip v on author.vip_id = v.vip_id +where vip_name='青铜作家' and story_name is null ; + +# 18. 找出写过作品浏览量大于5000的作家的所有作品中浏览量不到1000的作品信息(8分) +select * from story where views_number in(select views_number from story +where author_id in(select author_id from story where views_number>5000)) having views_number<1000; + +# 19. 查询所有小说的小说编号、小说名称、浏览量、分类名称、作者姓名、作者积分、作者等级名称,结果字段要用中文别名,并按浏览量降序排列,如果浏览量一样的,再按积分降序排列。(10分) +select story_id 小说编号,story_name 小说名称, views_number 浏览量, type.type_id 分类名称, author_name 作者名字, credits 作者积分, vip_name 作者等级名称 +from author,type,story,vip where author.author_id=story.author_id and vip.vip_id=author.vip_id and type.type_id=story.type_id order by views_number desc ,credits desc ; +``` \ No newline at end of file -- Gitee