diff --git "a/16 \346\236\227\346\230\200\350\220\261/20230306 \345\255\220\346\237\245\350\257\242.md" "b/16 \346\236\227\346\230\200\350\220\261/20230306 \345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..6ecf3bd1fb52af76436f20d91e2550888b32af5d --- /dev/null +++ "b/16 \346\236\227\346\230\200\350\220\261/20230306 \345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,85 @@ +# 笔记 + +子查询也称“内部查询”或者“嵌套查询”,是指**将一个 SELECT 查询(子查询)的结果作为另一个 SQL 语句(主查询)的数据来源或者判断条件**。 子查询可以嵌入 SELECT、INSERT、UPDATE 和 DELETE 语句中,也可以和 =、<、>、IN、BETWEEN、EXISTS 等运算符一起使用 + +# 作业 + +```sql +CREATE DATABASE n charset utf8; +use n; +CREATE TABLE stuinfo( +stuNO varchar(5), +stuName varchar(5), +stuSex enum('男','女'), +stuAge int, +stuAddress varchar(10), +stuSeat INT +); + +insert into stuinfo values +('s2501','张秋利','男',20,'美国硅谷',1), +('s2502','李斯文','女',18,'湖北武汉',2), +('s2503','马文才','男',18,'湖南长沙',3), +('s2504','欧阳俊雄','女',21,'湖北武汉',4), +('s2505','梅超风','男',20,'湖北武汉',5), +('s2506','陈旋风','男',19,'美国硅谷',6); + +create table stuExam( +examNO int, +stuNO varchar(5), +writtenExam int, +labExam INT); + +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); + +CREATE table stuMarks( +examNO int, +stuID varchar(5), +score int); + +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 b.stuNO,b.stuName,b.stuSex,MAX(a.score) from stuMarks a right JOIN stuinfo b on a.stuID = b.stuNO GROUP BY b.stuNO; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +SELECT b.stuNO,b.stuName,b.stuSex,(a.writtenExam+a.labExam)/2 考试平均分数 from stuExam a INNER JOIN stuinfo b on a.stuNO=b.stuNO +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +SELECT * from stuinfo where stuAge >= 20 and stuSex = '男'; +SELECT * from stuinfo where stuNO in (SELECT stuNO from stuinfo where stuAge >= 20 and stuSex = '男'); +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +SELECT * FROM stuinfo where stuAge >any (SELECT stuAge FROM stuinfo where stuSex = '男') and stuSex = '女'; +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select * from stuinfo inner join (select stuID,score from stumarks) a on stuinfo.stuNO=a.stuID where score>=60 +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.* from stuinfo a right join stumarks b on a.stuNO=b.stuID group by stuNO; +select a.* from stuinfo a where stuNO in (select stuID from stumarks); +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.*,score from stuinfo a left join stumarks b on a.stuNO=b.stuID where score is null group by stuNO; +select a.* from stuinfo a where stuNO not in (select stuID from stumarks); +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select a.*,score from stuinfo a right join stumarks b on a.stuNO=b.stuID where score>90; +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select * from stuinfo where stuNO=any (select stuID from stumarks group by stuID having avg(score)>80) +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select * from stuinfo a right join stumarks b on b.stuID =a.stuNO group by b.score having score> (select max(score) from (select a.stuName,score from stuinfo a right join stumarks b on b.stuID =a.stuNO group by b.score having a.stuName='张秋利') a); +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select * from stuinfo a right join stumarks b on b.stuID =a.stuNOgroup by b.score having score>any (select score from (select a.stuName,score from stuinfo a right join stumarks b on b.stuID =a.stuNO group by b.score having a.stuName='张秋利') a) and stuName!='张秋利'; +-- 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stuinfo where stuAge >any(select stuAge from stuinfo where stuSex='男')and stuSex='女'; +-- 14.查询出只是比某个男生年龄大的女生的信息 +select * from stuinfo where stuAge >any(select min(stuAge) from stuinfo where stuSex='男') and stuSex='女'; +``` \ No newline at end of file diff --git "a/16 \346\236\227\346\230\200\350\220\261/20230308 \347\273\203\344\271\240\351\242\230.md" "b/16 \346\236\227\346\230\200\350\220\261/20230308 \347\273\203\344\271\240\351\242\230.md" new file mode 100644 index 0000000000000000000000000000000000000000..ff41fbe368006b2adcccb6aa7ab6fe2d68c47725 --- /dev/null +++ "b/16 \346\236\227\346\230\200\350\220\261/20230308 \347\273\203\344\271\240\351\242\230.md" @@ -0,0 +1,256 @@ +# 第一份作业 + +```sql +CREATE database qaq CHARSET utf8; +use qaq; +CREATE table a( +orderID int, +orderDate date); + +INSERT into a 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 b( +itemID int, +orderid int, +itemType varchar(10), +itemName varchar(10), +theNumber int, +theMoney int); + +INSERT into b 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,a.orderDate,b.itemType,b.itemName,b.theNumber,b.theMoney from a right JOIN b on a.orderID = b.orderID; +-- 2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select a.orderID,a.orderDate,b.itemType,b.itemName from b left JOIN a on a.orderID = b.orderID where theNumber > 50; +3.查询所有的的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select a.orderID,a.orderDate,b.itemType,b.itemName,b.theNumber,b.theMoney,b.theNumber*b.theMoney total from a right JOIN b on a.orderID = b.orderID; +-- 4.查询单价大于等于5 或者 数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + select a.orderID,a.orderDate,b.itemType,b.itemName,b.theNumber,b.theMoney,b.theNumber*b.theMoney total from a right JOIN b on a.orderID = b.orderID where theNumber >= 50 or theMoney >=5; +-- 5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderid,count(itemType) from b 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 b group BY orderid,itemType; +``` + + + +# 第二份作业 + +```sql +-- # MySQL基础结课考试 +-- +-- ## 考试时间 120 分钟 总分:100分 +-- +-- **场景**: +-- +-- 你在一个软件公司上班,今天公司接一个新业务。要用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 '等级编号', +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) comment '等级编号' PRIMARY key, +vip_name varchar(20) comment '等级名称' unique key not null); +-- +-- 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) COMMENT '类型编号' PRIMARY key, +type_name varchar(20) comment '类型名称' UNIQUE key not null); +-- ### 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,'杜琦燕',428,'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 = 1005 or author_id = 1007; +-- 5. 作家杜子腾,写了一篇名为《拜登夸我很帅》的都市小说,有854个浏览量,请将这条信息插入到story表。(3分) +insert into story(author_id,story_name,views_number) values(1005,'拜登夸我很帅',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 author_id,story_name from story where views_number >8000; +-- 9. 查询积分大于1000 并且会员等级高于vip03的作家所有信息。(3分) +SELECT * from author where credits > 1000 and vip_id not in('VIP01','VIP02','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 vip_id 等级编号,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 story_id,story_name,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 DISTINCT b.author_name 姓名,c.vip_name 等级名称 from story a INNER JOIN (select * from author where vip_id = 'VIP02') as b on a.author_id = b.author_id INNER JOIN vip c on c.vip_id = b.vip_id where b.author_id not in (b.author_id); +-- 18. 找出写过作品浏览量大于5000的作家的所有作品中浏览量不到1000的作品信息(8分) +select a.* from story a inner JOIN (select * from story where views_number > 5000) as b on a.author_id = b.author_id where a.views_number < 1000; +-- 19. 说编查询所有小说的小号、小说名称、浏览量、分类名称、作者姓名、作者积分、作者等级名称,结果字段要用中文别名,并按浏览量降序排列,如果浏览量一样的,再按积分降序排列。(10分) +select a.story_id 小说编号,a.story_name 小说名称,a.views_number 浏览量,c.type_name 分类名称,b.author_name 作者姓名,b.credits 作者积分,d.vip_name 作者等级 from story a left JOIN author b on a.author_id = b.author_id left JOIN type c on a.type_id = c.type_id left join vip d on b.vip_id = d.vip_id ORDER BY a.views_number desc; +``` +