diff --git "a/06 \346\236\227\346\231\237\350\276\211/20230306 \345\255\220\346\237\245\350\257\242.sql" "b/06 \346\236\227\346\231\237\350\276\211/20230306 \345\255\220\346\237\245\350\257\242.sql" new file mode 100644 index 0000000000000000000000000000000000000000..140f1c9b6b648d71516b3cd43d0d332a35c96885 --- /dev/null +++ "b/06 \346\236\227\346\231\237\350\276\211/20230306 \345\255\220\346\237\245\350\257\242.sql" @@ -0,0 +1,114 @@ +# 一、笔记 + +## 1、select中嵌套子查询 + +​ 嵌套在另一个SQL语句中的查询,select语句可以嵌套在另一个select中,update,delete,insert,create等语句也可以,通常使用与where或having中 + +## 2、 SELECT的WHERE或HAVING中嵌套子查询 + +​ 如果子查询作为另一个SQL的过滤条件,通常把子查询嵌入到where或having中。 + +根据子查询结果不同可以分为三种情况 + +- 如果子查询是单列单值可以用比较运算符进行比较 +- 如果子查询是单列多值可以用in或者not in进行比较 +- 如果子查询是多列多值还可以用比较运算符搭配any、some、all等进行查询 + +## 3、select中嵌套exists型子查询 + +​ exists型子查询是可以存在外层select的where子句中,不过它和上面的where型子查询的工作模式不一样 + +​ 如果exists关键字后面的参数是任意一个子查询,系统会对子查询进行运算用来判断他要不要返回行,如果至少返回一行,那么exists的结果位true,那么外层查询语句进行查询,反之如果没有返回行,那么exists结果为false,外层查询语句不进行查询,exists和not exists的结果只取决于是否返回行不取决于这些行的内容。 + +​ 如果exists关键字后面的参数是一个关联子查询,那么子查询的where条件中包含与外层查询表的关联条件,那么此时将对外层查询表做循环,即在筛选外层查询表的每一条记录都看是否满足子查询的条件,如果满足那么就用外层进行二次筛选,否则丢弃这行记录。 + +​ + +# 二、作业 + +```MySQL +create database a1 charset utf8; + +use a1; + +create table stuinfo( +stuNO varchar(5), +stuName varchar(10), +stuSex enum('男','女'), +stuAge int, +stuAddress varchar(10), +stuSeat int +); + +create table stuExam( +examNO int , +stuNO varchar(5), +labExam int, +writtenExam int +); + +create table stuMarks( +examNO int, +stuID varchar(5), +score int +); + +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',82); + +-- 1.查询出年龄比班上平均年龄大的学生的信息 +select * from stuinfo where stuAge > (select avg(stuAge) from stuinfo); +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select stuNO,stuname,stuSex,ifnull(max(score),0) from stuinfo a left join stuMarks b on a.stuNO = b.stuID group by a.stuNO; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select a.stuNO,stuName,stuSex,(writtenExam+labExam)/2 考试平均分 from stuinfo a left join stuexam b on a.stuNO = b.stuNO; +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuSex='男' and stuAge>=20;-- 普通查询 +select * from stuinfo where stuNO in (select stuNO from stuinfo where stuSex='男' and stuAge>=20); -- 子查询 +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stusex='女' and stuage > (select max(stuage) from stuinfo where stusex='男'); +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select * from stumarks where stuid != (select stuid from stumarks where score<60) +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.* from stuinfo a inner join stumarks b on a.stuNO = b.stuID group by a.stuNO; -- 表连接 +select a.* from stuinfo a where stuno in (select a.stuNO from stuinfo a inner join stumarks b on a.stuNO = b.stuID group by a.stuNO); +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.* from stuinfo a left join stumarks b on a.stuNO = b.stuID where score is null group by a.stuNO; +select a.* from stuinfo a where stuno in (select a.stuno from stuinfo a left join stumarks b on a.stuNO = b.stuID where score is null group by a.stuNO); +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select a.* from stuinfo a inner join stumarks b on a.stuNO = b.stuID where score > 90 group by a.stuno; +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select * from stuinfo where stuno = (select stuid from stumarks group by stuid having avg(score) >80); +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +SELECT * FROM stuinfo WHERE +stuno = (SELECT stuID,score FROM stumarks WHERE stuid != 's2501' HAVING +score > ALL ( SELECT score FROM stumarks WHERE stuid = 's2501' )); +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select * from stuinfo where stuno =(select * from stumarks where stuid != 's2501' having score > any(select score from stumarks where stuid='s2501')); +-- 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stuinfo where stusex='女' and stuage > (select max(stuage) from stuinfo where stusex='男'); +-- 14.查询出只是比某个男生年龄大的女生的信息 +select * from stuinfo where stusex='女' and stuage > any(select stuage from stuinfo where stusex='男'); +``` + diff --git "a/06 \346\236\227\346\231\237\350\276\211/20230308 \347\273\203\344\271\240.md" "b/06 \346\236\227\346\231\237\350\276\211/20230308 \347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..efc86f234955109d982376bd996d707489c9ca5d --- /dev/null +++ "b/06 \346\236\227\346\231\237\350\276\211/20230308 \347\273\203\344\271\240.md" @@ -0,0 +1,341 @@ +一、练习二 + +```mysql +create database a2 charset utf8; + +use a2; + +create table table1( + orderid int, + orderdate date +); + +insert into table1 values + (1,'2008-01-12 00:00:00.000'), + (2,'2008-02-10 00:00:00.000'), + (3,'2008-02-15 00:00:00.000'), + (4,'2008-03-10 00:00:00.000'); + +create table table2( + itemid int, + orderid int, + itemtype varchar(10), + itemname varchar(10), + thenumber int, + themoney int +); + +insert into table2 values + (1,1,'文具','笔',72,2), + (2,1,'文具','尺',10,1), + (3,1,'体育用品','篮球',1,56), + (4,2,'文具','笔',36,2), + (5,2,'文具','固体胶',20,3), + (6,2,'日常用品','透明胶',2,1), + (7,2,'体育用品','羽毛球',20,3), + (8,3,'文具','订书机',20,3), + (9,3,'文具','订书针',10,3), + (10,3,'文具','裁纸刀',5,5), + (11,4,'文具','笔',20,2), + (12,4,'文具','信纸',50,1), + (13,4,'日常用品','毛巾',4,5), + (14,4,'日常用品','透明胶',30,1), + (15,4,'体育用品','羽毛球',20,3); + +-- 1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +SELECT + a.orderid, + orderdate, + itemtype, + itemname, + thenumber, + themoney +FROM + table1 a + RIGHT JOIN table2 b ON a.orderid = b.orderid; +-- 2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +SELECT + a.orderid, + orderdate, + itemtype, + itemname +FROM + table1 a + RIGHT JOIN table2 b ON a.orderid = b.orderid +WHERE + thenumber > 50; +-- 3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +SELECT + a.orderid, + orderdate, + itemtype, + itemname, + thenumber, + themoney, + thenumber*themoney total +FROM + table1 a + RIGHT JOIN table2 b ON a.orderid = b.orderid; +-- 4.查询单价大于等于5 或者 数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +SELECT + a.orderid, + orderdate, + itemtype, + itemname, + thenumber, + themoney, + thenumber*themoney total +FROM + table1 a + RIGHT JOIN table2 b ON a.orderid = b.orderid +WHERE + themoney >= 5; +-- 5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +SELECT + orderid, + count( itemid ) +FROM + table2 +GROUP BY + orderid;-- 6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- +-- 订单编号 产品类别 订购次数 总数量 +-- +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 20 +SELECT + orderid, + itemtype, + count( itemtype ), + sum( thenumber ) +FROM + table2 +GROUP BY + orderid, + itemtype; +``` + +# 二、大作业 + +```MySQL +-- 你在一个软件公司上班,今天公司接一个新业务。要用MySQL给一个小说网站设计一个数据库。 +-- + +-- **数据库名**:xiaoshuo +create database xiaoshuo charset utf8; +use xiaoshuo; + +-- 该数据库里有四张表:作家信息表 ( author )、作家等级信息表 ( vip )、小说作品信息表 ( story )、小说作品类型表 ( type ) +-- + +-- ### 1、相关表结构 +-- + +-- 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) unique key not null comment '作家姓名', + credits int comment '积分', + vip_id varchar(20) comment '等级编号' + +); + +-- 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) unique key not null comment '等级名称' + +); + +alter table author add foreign key (vip_id) references vip(vip_id); +-- + +-- 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) + +); + +-- 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) unique key not null comment'类型名称' + +); +alter table story add foreign key (type_id) references type(type_id); + +-- ### 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、题目 +-- 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 values (null,1005,'VIP01','拜登夸我很帅',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 not in('vip03','vip02','vip01'); +-- 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 vip_id,avg() 平均积分,count(*) 作家数量 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,story_id,type_id,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 author_name,vip_name from author a left join story b on a.author_id=b.author_id inner join vip c on a.vip_id=c.vip_id where vip_name ='白银作家' and story_name is null; +-- 18. 找出写过作品浏览量大于5000的作家的所有作品中浏览量不到1000的作品信息(8分) +select * from story where views_number <1000 and author_id in (select author_id from story where views_number>5000); +-- 19. 查询所有小说的小说编号、小说名称、浏览量、分类名称、作者姓名、作者积分、作者等级名称,结果字段要用中文别名,并按浏览量降序排列,如果浏览量一样的,再按积分降序排列。(10分) +select story_id 小说编号,story_name 小说名称,views_number 浏览量,type_name 分类名称,author_name 作者姓名,credits 作者积分,vip_name 作者等级名称 from author a inner join story b on a.author_id=b.author_id inner join type c on b.type_id=c.type_id inner join vip d on a.vip_id = d.vip_id order by views_number desc,credits desc; +``` +