From 2d36ce2d29fe2803145f93e03a8ad3d926e33d3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E5=BC=98=E6=99=96?= <351131303@qq.com> Date: Wed, 8 Mar 2023 14:53:02 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E9=83=91=E5=BC=98=E6=99=96=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E6=9F=A5=E8=AF=A2=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\345\222\214\344\275\234\344\270\232.md" | 208 ++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 "20 \351\203\221\345\274\230\346\231\226/20230306 \345\255\220\346\237\245\350\257\242\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" diff --git "a/20 \351\203\221\345\274\230\346\231\226/20230306 \345\255\220\346\237\245\350\257\242\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" "b/20 \351\203\221\345\274\230\346\231\226/20230306 \345\255\220\346\237\245\350\257\242\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" new file mode 100644 index 0000000..40d759a --- /dev/null +++ "b/20 \351\203\221\345\274\230\346\231\226/20230306 \345\255\220\346\237\245\350\257\242\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" @@ -0,0 +1,208 @@ +# 笔记 + +```sql +/* +子查询:嵌套在另一个SQL语句中的查询。 +SELECT语句可以嵌套在另一个SELECT中,UPDATE,DELETE,INSERT,CREATE语句等。 + +(1)SELECT的SELECT中嵌套子查询 +*/ + +#(1)在“t_employee”表中查询每个人薪资和公司平均薪资的差值, +#并显示员工薪资和公司平均薪资相差5000元以上的记录。 +SELECT ename AS "姓名", + salary AS "薪资", + ROUND((SELECT AVG(salary) FROM t_employee),2) AS "全公司平均薪资", + ROUND(salary-(SELECT AVG(salary) FROM t_employee),2) AS "差值" +FROM t_employee +WHERE ABS(ROUND(salary-(SELECT AVG(salary) FROM t_employee),2))>5000; + +#(2)在“t_employee”表中查询每个部门平均薪资和公司平均薪资的差值。 +SELECT did,AVG(salary), +AVG(salary)-(SELECT AVG(salary) FROM t_employee) +FROM t_employee +GROUP BY did; +/* +子查询嵌套在where后面。 +在where或having后面的子查询结果是: +(1)单个值,那么可以用=,>,<,>=,<=,!=这样的运算符和子查询的结果做比较 +(2)多个值,那么需要用in,not in, >all,>any....形式做比较 + 如“<”、“<=”、“>”、“>=”、“=”、“!=”等搭配ANY、SOME、ALL等关键字与查询结果进行比较 + +*/ +#(1)在“t_employee”表中查询薪资最高的员工姓名(ename)和薪资(salary)。 +#SELECT ename,MAX(salary) FROM t_employee;#错误 +#取表中第一行员工的姓名和全公司最高的薪资值一起显示。 + +SELECT ename,salary +FROM t_employee +WHERE salary = (SELECT MAX(salary) FROM t_employee); + +#(2)在“t_employee”表中查询比全公司平均薪资高的男员工姓名和薪资。 +SELECT ename,salary +FROM t_employee +WHERE salary > (SELECT AVG(salary) FROM t_employee) AND gender = '男'; + +#(3)在“t_employee”表中查询和“白露”,“谢吉娜”同一部门的员工姓名和电话。 +SELECT ename,tel,did +FROM t_employee +WHERE did IN(SELECT did FROM t_employee WHERE ename='白露' || ename='谢吉娜'); + +SELECT ename,tel,did +FROM t_employee +WHERE did =ANY(SELECT did FROM t_employee WHERE ename='白露' || ename='谢吉娜'); + + +#(4)在“t_employee”表中查询薪资比“白露”,“李诗雨”,“黄冰茹”三个人的薪资都要高的员工姓名和薪资。 +SELECT ename,salary +FROM t_employee +WHERE salary >ALL(SELECT salary FROM t_employee WHERE ename IN('白露','李诗雨','黄冰茹')); + + +#(5)查询“t_employee”和“t_department”表,按部门统计平均工资, +#显示部门平均工资比全公司的总平均工资高的部门编号、部门名称、部门平均薪资, +#并按照部门平均薪资升序排列。 +SELECT t_department.did,dname,AVG(salary) +FROM t_employee RIGHT JOIN t_department +ON t_employee.did = t_department.did +GROUP BY t_department.did +HAVING AVG(salary) >(SELECT AVG(salary) FROM t_employee) +ORDER BY AVG(salary); +#exist型子查询 +/* +(1)exists()中的子查询和外面的查询没有联合的情况下, +如果exists()中的子查询没有返回任何行,那么外面的子查询就不查了。 +(2)exists()中的子查询与外面的查询有联合工作的情况下, +循环进行把外面查询表的每一行记录的值,代入()中子查询,如果可以查到结果, +就留下外面查询的这条记录,否则就舍去。 +*/ + +#(1)查询“t_employee”表中是否存在部门编号为NULL的员工, +#如果存在,查询“t_department”表的部门编号、部门名称。 +SELECT * FROM t_department +WHERE EXISTS(SELECT * FROM t_employee WHERE did IS NULL); + +#(2)查询“t_department”表是否存在与“t_employee”表相同部门编号的记录, +#如果存在,查询这些部门的编号和名称。 +SELECT * FROM t_department +WHERE EXISTS(SELECT * FROM t_employee WHERE t_employee.did = t_department.did); + +#查询结果等价于下面的sql +SELECT DISTINCT t_department.* +FROM t_department INNER JOIN t_employee +ON t_department.did = t_employee.did; +#子查询嵌套在from后面 +/* +当一个查询要基于另一个查询结果来筛选的时候, +另一个查询还是多行多列的结果,那么就可以把这个查询结果当成一张临时表, +放在from后面进行再次筛选。 + +*/ + +#(1)在“t_employee”表中,查询每个部门的平均薪资, +#然后与“t_department”表联合查询 +#所有部门的部门编号、部门名称、部门平均薪资。 + +SELECT did,AVG(salary) FROM t_employee GROUP BY did; + ++------+-------------+ +| did | AVG(salary) | ++------+-------------+ +| 1 | 11479.3125 | +| 2 | 13978 | +| 3 | 37858.25 | +| 4 | 12332 | +| 5 | 11725 | ++------+-------------+ +5 ROWS IN SET (0.00 sec) + +#用上面的查询结果,当成一张临时表,与t_department部门表做联合查询 +#要给这样的子查询取别名的方式来当临时表用,不取别名是不可以的。 +#而且此时的别名不能加"" +#字段的别名可以加"",表的别名不能加"" + +SELECT t_department.did ,dname,AVG(salary) +FROM t_department LEFT JOIN (SELECT did,AVG(salary) FROM t_employee GROUP BY did) temp +ON t_department.did = temp.did; +#错误,from后面的t_department和temp表都没有salary字段, +#SELECT t_department.did ,dname,AVG(salary)出现AVG(salary)是错误的 + +SELECT t_department.did ,dname,pingjun +FROM t_department LEFT JOIN (SELECT did,AVG(salary) AS pingjun FROM t_employee GROUP BY did) temp +ON t_department.did = temp.did; + + +#(2)在“t_employee”表中查询每个部门中薪资排名前2的员工姓名、部门编号和薪资。 +SELECT * FROM ( +SELECT ename,did,salary, +DENSE_RANK() over (PARTITION BY did ORDER BY salary DESC) AS paiming +FROM t_employee) temp +WHERE temp.paiming <=2; +``` + + + +# 作业 + +```sql +create database abc charset utf8; +use abc; +create table stuinfo( +stuNO varchar(10), +stuName varchar(10), +stuSex enum('男','女'), +stuAge int, +sutAddress varchar(20), +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(10), +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(10), +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 a.stuNo,stuName,stuSex,max(score) from stuinfo a inner join stuMarks b on a.stuNo=b.stuID group by b.stuID; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select a.stuNO,stuName,stuSex,c.b from stuinfo a inner join (select stuNO,(writtenExam+labExam)/2 b from stuExam group by stuNO) as c on a.stuNO=c.stuNO; +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +-- 普通查询 +select * from stuinfo where stuSex='男' and stuAge>=20; +-- 子查询 +select * from stuinfo where stuNO =any (select stuNO from stuinfo where stuSex='男' and stuAge>=20); +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stuAge>any(select max(stuAge) from stuinfo where stuSex='男') and stuSex='女'; +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select * from stuinfo a left join (select stuID,min(score) b from stuMarks GROUP BY stuID) c on c.stuID = a.stuNO where b>=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 in (select stuid from stumarks where score > all(select score from stumarks where stuid = (select stuno from stuinfo where stuname = '张秋利'))); +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +-- 看不明白 +-- 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='男'); + +``` + -- Gitee From d60751441242917731b6baec07a340fc686f1966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E5=BC=98=E6=99=96?= <351131303@qq.com> Date: Wed, 8 Mar 2023 20:07:03 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E9=83=91=E5=BC=98=E6=99=96=E7=9A=84?= =?UTF-8?q?=E4=B8=A4=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...8 \347\254\254\344\270\200\344\273\275.md" | 63 +++++++++++++++++++ ...8 \347\254\254\344\272\214\344\273\275.md" | 63 +++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 "20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\270\200\344\273\275.md" create mode 100644 "20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\272\214\344\273\275.md" diff --git "a/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\270\200\344\273\275.md" "b/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\270\200\344\273\275.md" new file mode 100644 index 0000000..e662f23 --- /dev/null +++ "b/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\270\200\344\273\275.md" @@ -0,0 +1,63 @@ +```sql +create database zz charset utf8; +use zz; +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( +itemlID int, +orderid int, +itemType varchar(10), +itemName varchar(5), +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 b.orderid,orderDate,itemType,itemName,theNumber,theMoney from a inner join b on a.orderID=b.orderid; +-- 2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select b.orderid,itemType,itemName from a inner join b on a.orderID=b.orderid where theNumber>50; +-- 3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select b.orderid,orderDate,itemType,itemName,theNumber,theMoney,(theNumber*theMoney) as zongjia from a inner join b on a.orderID=b.orderid; +-- 4.查询单价大于等于5 或者 数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select b.orderid,orderDate,itemType,itemName,theNumber,theMoney,(theNumber*theMoney) as zongjia from a inner join b on a.orderID=b.orderid where theMoney>=5 or theNumber>=50; +-- 5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderid 编号,count(theNumber) from b group by 编号; +-- 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 订单编号,产品类别; +``` + diff --git "a/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\272\214\344\273\275.md" "b/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\272\214\344\273\275.md" new file mode 100644 index 0000000..e662f23 --- /dev/null +++ "b/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\272\214\344\273\275.md" @@ -0,0 +1,63 @@ +```sql +create database zz charset utf8; +use zz; +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( +itemlID int, +orderid int, +itemType varchar(10), +itemName varchar(5), +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 b.orderid,orderDate,itemType,itemName,theNumber,theMoney from a inner join b on a.orderID=b.orderid; +-- 2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select b.orderid,itemType,itemName from a inner join b on a.orderID=b.orderid where theNumber>50; +-- 3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select b.orderid,orderDate,itemType,itemName,theNumber,theMoney,(theNumber*theMoney) as zongjia from a inner join b on a.orderID=b.orderid; +-- 4.查询单价大于等于5 或者 数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select b.orderid,orderDate,itemType,itemName,theNumber,theMoney,(theNumber*theMoney) as zongjia from a inner join b on a.orderID=b.orderid where theMoney>=5 or theNumber>=50; +-- 5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 +select orderid 编号,count(theNumber) from b group by 编号; +-- 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 订单编号,产品类别; +``` + -- Gitee From ff1fbcd9f1b81b61195fd762624773cc202ba3b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E5=BC=98=E6=99=96?= <351131303@qq.com> Date: Wed, 8 Mar 2023 20:25:11 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=B8=A4=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230308 \347\254\2541\344\273\275.md" | 0 .../20230308 \347\254\2542\344\273\275.md" | 197 ++++++++++++++++++ ...8 \347\254\254\344\272\214\344\273\275.md" | 63 ------ 3 files changed, 197 insertions(+), 63 deletions(-) rename "20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\270\200\344\273\275.md" => "20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\2541\344\273\275.md" (100%) create mode 100644 "20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\2542\344\273\275.md" delete mode 100644 "20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\272\214\344\273\275.md" diff --git "a/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\270\200\344\273\275.md" "b/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\2541\344\273\275.md" similarity index 100% rename from "20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\270\200\344\273\275.md" rename to "20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\2541\344\273\275.md" diff --git "a/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\2542\344\273\275.md" "b/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\2542\344\273\275.md" new file mode 100644 index 0000000..b8919b3 --- /dev/null +++ "b/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\2542\344\273\275.md" @@ -0,0 +1,197 @@ +```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) | 等级编号,非空、外键关联等级信息表 | +-- +-- 2. 作家等级信息表 ( vip ) (3分) +-- +-- | 字段名称 | 数据类型 | 说明及要求 | +-- | -------- | ----------- | ------------------------ | +-- | vip_id | varchar(20) | 等级编号,主键 | +-- | vip_name | varchar(20) | 等级名称,非空,不能重复 | +-- +-- +-- 3. 小说作品信息表 ( story )(4分) +-- +-- | 字段名称 | 数据类型 | 说明及要求 | +-- | ------------ | ----------- | ----------------------------- | +-- | story_id | int | 作品编号,主键,自增 | +-- | author_id | int | 作家编号,外键,关联作家信息表 | +-- | type_id | varchar(20) | 类型编号,外键,关键作品类型表 | +-- | story_name | varchar(50) | 作品名称 | +-- | views_number | int | 浏览量 | +-- +-- 4. 小说作品类型表 ( type )(2分) +-- +-- +-- | 字段名称 | 数据类型 | 说明及要求 | +-- | --------- | ----------- | ------------------------ | +-- | type_id | varchar(20) | 类型编号,主键 | +-- | type_name | varchar(20) | 类型名称,非空,不能重复 | +-- +-- ### 2、对应的表数据 +-- +-- 1. 作家信息表 (4分) +-- +-- | 作家编号 | 作家名称 | 积分 | 等级编号 | +-- | :------: | :------: | :--: | :------: | +-- | 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 | 钻石作家 | +-- +-- 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 | +-- +-- 4. 作品类型(3分) +-- + +-- | 类型编号 | 类型名称 | +-- | :------: | :------: | +-- | L01 | 玄幻 | +-- | L02 | 奇幻 | +-- | L03 | 武侠 | +-- | L04 | 仙侠 | +-- | L05 | 都市 | +-- +-- +-- ### 3、题目 +-- +-- > 所有题目要求使用SQL语句完成 +-- +-- 1. 根据前面提供的表结构和表数据,创建数据库并分别创建这张四张表;并插入相关数据。(提醒:外键请注意建表顺序和插入数据的顺序) (30分) +create table author( +author_id int primary key comment'作家编号', +author_name varchar(20) not null unique key comment'作家姓名', +credits int comment'积分', +vip_id varchar(20) not null comment'等级编号', +foreign key(vip_id) references vip(vip_id) +); +create table vip( +vip_id varchar(20) primary key comment'等级编号', +vip_name varchar(20) not null unique key comment'等级名称' +); +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) +); +create table type( +type_id varchar(20) primary key comment'类型编号', +type_name varchar(20) not null unique key comment'类型名称' +); +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'); +insert into vip values +('VIP01','青铜作家'), +('VIP02','白银作家'), +('VIP03','黄金作家'), +('VIP04','钻石作家'); +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); +insert into type values +('L01','玄幻'), +('L02','奇幻'), +('L03','武侠'), +('L04','仙侠'), +('L05','都市'); +-- 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; +``` + diff --git "a/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\272\214\344\273\275.md" "b/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\272\214\344\273\275.md" deleted file mode 100644 index e662f23..0000000 --- "a/20 \351\203\221\345\274\230\346\231\226/20230308 \347\254\254\344\272\214\344\273\275.md" +++ /dev/null @@ -1,63 +0,0 @@ -```sql -create database zz charset utf8; -use zz; -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( -itemlID int, -orderid int, -itemType varchar(10), -itemName varchar(5), -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 b.orderid,orderDate,itemType,itemName,theNumber,theMoney from a inner join b on a.orderID=b.orderid; --- 2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 -select b.orderid,itemType,itemName from a inner join b on a.orderID=b.orderid where theNumber>50; --- 3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 -select b.orderid,orderDate,itemType,itemName,theNumber,theMoney,(theNumber*theMoney) as zongjia from a inner join b on a.orderID=b.orderid; --- 4.查询单价大于等于5 或者 数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 -select b.orderid,orderDate,itemType,itemName,theNumber,theMoney,(theNumber*theMoney) as zongjia from a inner join b on a.orderID=b.orderid where theMoney>=5 or theNumber>=50; --- 5.查询每个订单分别订购了几个产品,例如: --- 编号 订购产品数 --- 1 3 --- 2 4 -select orderid 编号,count(theNumber) from b group by 编号; --- 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 订单编号,产品类别; -``` - -- Gitee