diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..674d67d8e0b79de1929976e3cbb3f9d1cd5d7a43 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/mysql-base.iml b/.idea/mysql-base.iml new file mode 100644 index 0000000000000000000000000000000000000000..d6ebd4805981b8400db3e3291c74a743fef9a824 --- /dev/null +++ b/.idea/mysql-base.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000000000000000000000000000000000000..f7684940a797dc9a4332f319a882501906e73939 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + 1678249995692 + + + + + + + \ No newline at end of file diff --git "a/13 \346\264\252\351\224\246\346\264\213/20230308 \346\264\252\351\224\246\346\264\213\344\275\234\344\270\232\344\272\214.md" "b/13 \346\264\252\351\224\246\346\264\213/20230308 \346\264\252\351\224\246\346\264\213\344\275\234\344\270\232\344\272\214.md" new file mode 100644 index 0000000000000000000000000000000000000000..bb12219c28d5b4b3f62836d37ff5c8e2a48e185f --- /dev/null +++ "b/13 \346\264\252\351\224\246\346\264\213/20230308 \346\264\252\351\224\246\346\264\213\344\275\234\344\270\232\344\272\214.md" @@ -0,0 +1,95 @@ +```sql +create database hjy charset utf8 +use hjy; +create table time( + orderID int primary key auto_increment, + orderDate datetime +); +insert into time 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'); +select * from time; +create table wenju( + itemID INT PRIMARY KEY auto_increment, + orderid int, + itemType varchar(10), + itemName varchar(10), + theNumer int, + theMoney INT +); +insert into wenju 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'); +select * from wenju; +``` + +~~~sql +-- 根据图示,完成下列题目: + +-- 1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 + +```sql +select wenju.orderid,wenju.orderid,itemtype,itemname,theNumer,themoney,TIME.ORDERDATE from wenju left join time on wenju.orderid = time.orderid; +``` + +-- 2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 + +```sql +SELECT * from wenju left join time on wenju.itemid = time.orderid where thenumer >50; +``` + +-- 3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +```sql +select wenju.orderid,orderdate,itemtype,itemname from wenju left join time on wenju.itemid = time.orderid; + +``` + +-- 4.查询单价大于等于5 或者 数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 + +```sql +select wenju.orderid,orderdate,itemtype,itemname,thenumer,themoney,thenumer*themoney 总价 from wenju left join time on wenju.orderid = time.orderid where themoney >5 or thenumer > 50; + +``` + +-- 5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 +-- 1 3 +-- 2 4 + +```sql +select wenju.orderid,thenumer from wenju left join time on wenju.orderid=time.orderid; +``` + + +-- 6.查询每个订单里的每个类别的产品分别订购了几次和总数量,例如: +-- + +-- 订单编号 产品类别 订购次数 总数量 +-- + +-- 1 文具 2 82 +-- 1 体育用品 1 1 +-- 2 文具 2 56 +-- 2 体育用品 1 2 +-- 2 日常用品 1 2 +select wenju.orderid 订单编号,itemtype 产品类别,thenumer 总次数 from wenju left join time on wenju.orderid = time.orderid; +~~~ + + + + + diff --git "a/13 \346\264\252\351\224\246\346\264\213/20230308 \346\264\252\351\224\246\346\264\213\345\244\247\344\275\234\344\270\232.md" "b/13 \346\264\252\351\224\246\346\264\213/20230308 \346\264\252\351\224\246\346\264\213\345\244\247\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..61175a4ca8b245d18e88a5e1f7ea422f6bc149a1 --- /dev/null +++ "b/13 \346\264\252\351\224\246\346\264\213/20230308 \346\264\252\351\224\246\346\264\213\345\244\247\344\275\234\344\270\232.md" @@ -0,0 +1,135 @@ + + +```sql +-- 1. 根据前面提供的表结构和表数据,创建数据库并分别创建这张四张表;并插入相关数据。(提醒:外键请注意建表顺序和插入数据的顺序) (30分) +create database xiaoshuo charset utf8; +use xiaoshuo; +create table author( + author_id int comment '作家编号' primary key, + author_name varchar(20) comment '作家姓名' not null unique key, + credits int comment '积分', + vip_id varchar(20) comment '等级编号' not null, + foreign key author(vip_id) references vip(vip_id) +); + +create table vip( + vip_id varchar(20) comment '等级编号' primary key, + vip_name varchar(20) comment '等级名称' not null unique key +); + +create table story( + story_id int comment '作品编号' primary key auto_increment, + 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) comment '类型编号' primary key, + type_name varchar(20) comment '类型名称' not null unique key +); + +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 column 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 values('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 author_id 作者编号,story_name 作品名称 +from story where views_number>8000; +-- 9. 查询积分大于1000 并且会员等级高于vip03的作家所有信息。(3分) +select * from author a where 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; +-- 12. 查询出小说的总浏览量,最高浏览量,最小浏览量,平均浏览量,给字段用上中文别名。(3分) +select sum(views_number) 浏览量,max(views_number) 最高浏览量,min(views_number) 最小浏览量,avg(views_number) 平均浏览量 +from story; +-- 13. 查询各种等级的作家的平均积分和作家数量,并对查询结果使用中文别名。(3分) +select avg(credits) 平均积分,count(author_name) 作家数量 +from author +group by vip_id +-- 14. 查询小说数量大于等于2的分类编号和小说数量。(4分) +select type_id 分类编号,count(story_name) 小说数量 +from story +group by type_id +having count(story_name)>=2; +-- 15. 查询所有小说中浏览量最少的书的作品编号、作品名称和类型编号、浏览量。(4分) +select story_id 作品编号,story_name 作品名称,type_id 类型编号,views_number 浏览量 +from story +having min(views_number); +-- 16. 查询积分比刘产高的作者所有信息。(5分) +select * from author +where credits>(select credits from author where author_name='刘产'); +-- 17. 查询出哪些白银作家是没有写小说的,显示这些作家的姓名、等级名称。(8分) +select * from story a right join +(select author_id,vip_name +from author a inner join vip b +on a.vip_id=b.vip_id +where vip_name=' 白银作家') b +on a.author_id=b.author_id +-- 18. 找出写过作品浏览量大于5000的作家的所有作品中浏览量不到1000的作品信息(8分) +select * from story a right join +(select * from author a where author_id=any +(select author_id from story +where views_number>5000) +) b +on a.author_id=b.author_id +where views_number<1000 +-- 19. 查询所有小说的小说编号、小说名称、浏览量、分类名称、作者姓名、作者积分、作者等级名称,结果字段要用中文别名,并按浏览量降序排列,如果浏览量一样的,再按积分降序排列。(10分) +select c.story_id 小说编号,c.story_name 小说名称,views_number 浏览量,d.type_name 分类名称,a.author_name 作者姓名,a.credits 作者积分,b.vip_name 作者等级名称 from +author a left join vip b +on a.vip_id=b.vip_id +left join story c +on a.author_id=c.author_id +left join type d +on c.type_id=d.type_id +group by views_number,a.credits + + +``` + diff --git "a/13 \346\264\252\351\224\246\346\264\213/20230308 \346\264\252\351\224\246\346\264\213\346\237\245\350\257\242\344\275\234\344\270\232.md" "b/13 \346\264\252\351\224\246\346\264\213/20230308 \346\264\252\351\224\246\346\264\213\346\237\245\350\257\242\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..24f833181920e22ee140e663845c8f9d6365435d --- /dev/null +++ "b/13 \346\264\252\351\224\246\346\264\213/20230308 \346\264\252\351\224\246\346\264\213\346\237\245\350\257\242\344\275\234\344\270\232.md" @@ -0,0 +1,193 @@ +笔记 + +sql +-- 什么子查询 +-- 嵌套在其它SQL语句中的select语句 +-- 子查询通常在外层语句之前优先执行,所以要用()包括起来 +-- 子查询的结果,一般作为外层语句的条件,数据源等 + +select ① from ③ where ②; + +# 形式一,select后面嵌套子查询 +#(1)在“t_employee”表中查询每个人薪资和公司平均薪资的差值, +#并显示员工薪资和公司平均薪资相差5000元以上的记录。 +select avg(salary) from t_employee; + +select ename,salary,salary-(select avg(salary) from t_employee) 差值 from t_employee +where abs(salary-(select avg(salary) from t_employee))>5000; -- where 后面不支持聚合函数和别名,having支持聚合也支持别名 + +#(2)在“t_employee”表中查询每个部门平均薪资和公司平均薪资的差值。 +# 第一步,查询每个部门的平均薪资 +select did,avg(salary),avg(salary)-(select avg(salary) from t_employee) 差值 from t_employee group by did; +# 第二步,查询全公司的平均薪资 + + +# 形式二,在where/having 后面做条件 +#(1)在“t_employee”表中查询薪资最高的员工姓名(ename)和薪资(salary)。 +# 第一步,查询最高的薪资是多少钱 +select max(salary) from t_employee; +# 第二步,查询谁的薪资等于最高的这个值 +select * from t_employee where salary= (select max(salary) from t_employee); + + +#(2)在“t_employee”表中查询比全公司平均薪资高的男员工姓名和薪资。 +# 第一步,查询全公司平均的薪资是多少 +select avg(salary) from t_employee; + +# 第二步,查询谁的薪资>平均薪资 && gender = '男' +select * from t_employee +where gender = '男' + and salary > (select avg(salary) from t_employee); + + +#(3)在“t_employee”表中查询和“白露”,“谢吉娜”同一部门的员工姓名和电话。 +# 第一步,查询“白露”,“谢吉娜”的部门编号 +select did from t_employee where ename = '白露' or ename = '谢吉娜'; + +# 第二步,谁的部门编号和她们两一样 +-- select ename,tel from t_employee +-- where did = (select did from t_employee where ename = '白露' or ename = '谢吉娜'); +-- 1242 - Subquery(子查询) returns (返回) more than 1 row (超过1行) + +select ename,tel from t_employee +where did in(select did from t_employee where ename = '白露' or ename = '谢吉娜'); + + +select ename,tel from t_employee +where did =any(select did from t_employee where ename = '白露' or ename = '谢吉娜'); + +/* + 当子查询放在where 、 having 后面当条件用是,有几种情况 + ① 单列单个值,可以直接用>,<,<=...!=,这些比较运算符 + ② 单列多个值,要用in,not in 这种集合的比较 + ③ 单列多个值,可以用>,<,<=...!=,比较运算符,搭配any,all 这些关键字,一起使用 + + +*/ + + + +# 形式三,在from后面,形成一个临时表,必须加一个别名 +#(1)查询每个部门的部门编号、部门名称,平均薪资, +# 方案一,先联表,再分组 +select b.did,dname,avg(salary) +from t_employee a right join t_department b on a.did = b.did +group by b.did; + +# 方案二,先分组,再联表 +select b.did,dname,num +from +(select did,avg(salary) num from t_employee group by did) as avgtable +right join t_department b on avgtable.did = b.did +order by did; + +/* + 子查询的结果: + 单列单个值:可以放在select 后,也可以放where/having之后 + 单列多个值:where后,或having后 ,不用直接用<,=,>= 这种单纯的比较运算符。但可以用搭配any(任意一个),all(所有的)等关键字一起使用。还可以用in,not in这种表达式 + 多列时,只能当临时表来表用,放在from后面,而且必须,给他取个别名 +```sql + +作业 + +```sql +CREATE DATABASE ku1 CHARSET utf8; +use ku1; +#foreign key (从表的某个字段) references 主表名(被参考字段) +CREATE TABLE stuinfo( +stuNO VARCHAR(20) PRIMARY KEY, +stuName VARCHAR(20), +stuSex VARCHAR(10), +stuAge VARCHAR(20), +stuAddress VARCHAR(20), +stuSeat int(11) +); +drop TABLE stuinfo; + +CREATE table stuExam( +examNO INT(11) PRIMARY KEY, +stuNO VARCHAR(20), +writtenExam int(11), +labExam int(11), +FOREIGN key (stuNO) REFERENCES stuinfo(stuNO), +FOREIGN key (examNO) REFERENCES stuMarKs (examNO) +); +DROP TABLE stuExam; + +CREATE TABLE stuMarKs( +examNO INT(11) PRIMARY KEY, +stuID VARCHAR(20), +score INT(11) +); +DROP TABLE stuMarKs; + + +INSERT INTO stuinfo VALUES('s2501','张秋利','男','20','美国硅谷',1), +('s2502','李斯文','女','18','湖北武汉',2), +('s2503','马文才','男','22','湖南长沙',3), +('s2504','欧阳俊雄','女','21','湖北武汉',4), +('s2505','梅超风','男','20','湖北武汉',5), +('s2506','陈旋风','男','19','美国硅谷',6); + +SELECT * from stuinfo; + +INSERT into stuExam VALUES(1,'s2501',50,70), +(2,'s2502',60,68), +(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 * from stuinfo INNER JOIN stuExam on stuinfo.stuNO=stuExam.stuNO; +select stuId,stuname, stuSex from stuMarKs right join stuexam on stuExam.examNO=stuMarKs.examNO right join stuinfo on stuinfo.stuNO=stuExam.stuNO; +select stuNO,stuName,stuSex,sco +from stuinfo left join +(select stuID,max(score) sco from stumarks group by stuID) a +on stuinfo.stuNO=a.stuID; + +SELECT stuinfo.stuNO,stuMarKs.MAX(score) 最高 FROM stuinfo INNER JOIN stuExam on stuinfo.stuNO=stuExam.stuNO RIGHT JOIN stuMarKs ON stuExam.stuNO=stuMarKs.stuID; + +select a.stuno,a.stuname,a.stusex,max(b.score)from stuinfo as a left join stumarks as b on a.stuno = b.stuid group by a.stuno; +#3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +SELECT stuinfo.stuNO,stuinfo.stuName,stuSex,AVG(writtenExam)from stuinfo INNER JOIN stuExam on stuinfo.stuNO=stuExam.stuNO; +#4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select *from stuinfo where stuage>=20 and stusex='男'; +select * from (select *from stuinfo where stuage>=20 and stusex='男') as s; +#5.查询出年龄比所有男生年龄都大的女生的信息 +SELECT MAX(stuAge) from stuinfo WHERE stuSex='女'; +SELECT * from stuinfo WHERE stuAge>(SELECT MAX(stuAge) from stuinfo WHERE stuSex='女'); +#6.查询出所有选修课程都及格的学生的信息 (stuMarks)SELECT + +select stuinfo.* from stuMarKs right join stuexam on stuexam.examNo=stuMarKs.examNo right join stuinfo on stuinfo.stuNo=stuexam.stuNo where score>60; +#7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +SELECT * from stuMarKs right join stuinfo on stuinfo.stuNo=stuMarKs.stuid where score is not null group by stuname; +select * from stuinfo where stuno in(select stuid from stuMarKs); +#8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +#9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select a.stuno,a.stuname,a.stusex,max(b.score) from stuinfo as a left join stumarks as b on a.stuno = b.stuid group by a.stuno; + + +#10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +SELECT * from stuexam WHERE ; +#11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) + +#12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select MAX(score) from stuMarKs; +SELECT * from stumarks where ; +#13.查询班上比所有男生年龄都要大的女生的信息 +SELECT * from stuinfo WHERE stuAge>(SELECT MAX(stuAge) from stuinfo WHERE stuSex='女'); +#14.查询出只是比某个男生年龄大的女生的信息 +SELECT MIN(stuAge) from stuinfo WHERE stuSex='男'; +SELECT * from stuinfo WHERE stuAge>(SELECT MIN(stuAge) from stuinfo WHERE stuSex='男') AND stuSex='女'; \ No newline at end of file