diff --git "a/31 \351\231\210\347\201\265\351\222\260/2023.0306 \345\255\220\346\237\245\350\257\242\347\254\224\350\256\260\344\275\234\344\270\232.md" "b/31 \351\231\210\347\201\265\351\222\260/2023.0306 \345\255\220\346\237\245\350\257\242\347\254\224\350\256\260\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..1730156bff669b964b42f8a4e9b537d4d99c2add --- /dev/null +++ "b/31 \351\231\210\347\201\265\351\222\260/2023.0306 \345\255\220\346\237\245\350\257\242\347\254\224\350\256\260\344\275\234\344\270\232.md" @@ -0,0 +1,139 @@ +```mysql +create database ly11 charset utf8; +use lyll; +create table stuinfo( +stuNO varchar(6) , +stuName varchar(6), +stuSex varchar(4), +stuAge varchar(4) , +stuAddress varchar(10), +stuSeat varchar(4) +); +desc stuinfo; +INSERT into stuinfo values('s2501','张秋利','男',20,'美国硅谷',1), + ('s2502','李斯才','女',18,'湖北武汉',2), +('s2503','马文才','男',18,'湖南长沙',3), +('s2504','欧阳俊雄','女',21,'湖北武汉',4), +('s2505','梅超风','男',20,'湖北武汉',5), +('s2506','陈旋风','男',19,'美国硅谷',6); +select *from stuinfo; + +create table stuExam( +examNO varchar(3), +stuNO varchar(6), +writtenExam varchar(4), +labExam varchar(4) +); +desc stuExam; +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'); + +select * from stuExam ; + +create table stuMarks( +examNO varchar(3), +stuID varchar(6), +score varchar(4) +); +desc stuMarks; +INSERT into stuMarks values +('1','s2501','88'), +('2','s2502','92'), +('3','s2503','53'), +('4','s2504','60'), +('5','s2505','99'), +('6','s2506','82'); + + +select * from stuMarks ; + +-- 在如图的数据表上完成以下题目 + +-- 1.查询出年龄比班上平均年龄大的学生的信息 + +SELECT * from stuinfo where stuAge >(SELECT avg(stuAge) from stuinfo); + +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) + SELECT max(score) from stuMarks; + SELECT stuinfo.stuNO, stuName, stuSex, (SELECT max(score) from stuMarks ) from stuinfo left join stuMarks on stuinfo.stuNO=stuMarks.stuID ; + + +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +SELECT (labExam+writtenExam)/2 from stuExam ; +SELECT stuinfo.stuNO, stuName, stuSex,(SELECT (labExam+writtenExam)/2 ) from stuinfo left join stuExam on stuinfo.stuNO=stuExam.stuNO ; + +-- +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +SELECT * from stuinfo where stuSex='男'and stuAge>=20; +select * from (select * from stuinfo where stuage>=20) a where stusex='男'; + +-- 5.查询出年龄比所有男生年龄都大的女生的信息 + +SELECT * from stuinfo where stuSex='女' and stuAge>all(SELECT stuAge from stuinfo where stuSex='男'); + +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) + +SELECT * from stuinfo left join stuMarks on stuinfo.stuNO=stuMarks.stuID where score>=60; + +select * from stuinfo a INNER JOIN stumarks b on a.stuNo = b.stuID where a.stuNo != (select stuid from stumarks where score<60); +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +SELECT * FROM stuinfo a inner join stumarks b on a.stuNo = b.stuID; + +-- + +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) + +SELECT * FROM stuinfo a inner join stumarks b on a.stuNo = b.stuID where score is null; + +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) + +SELECT * from stuinfo inner join stuMarks on stuinfo.stuNO=stuMarks.stuID where stuMarks.score>90; + +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuExam) + +SELECT * from stuexam where stuexam.score >((labExam+writtenExam)/2 ); + +select * from stuinfo where stuno = (select stuno from stuexam where (writtenExam+labExam)/2>80); + +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) + +select * from stuinfo a left join stuExam b on a.stuNo = b.stuNo where writtenExam>(select writtenExam from stuExam where stuNo=(select stuNo from stuinfo where stuname='张秋利')) AND labExam>(select labExam from stuExam where stuNo=(select stuNo from stuinfo where stuname='张秋利')); + +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) + +select * from stuinfo a left join stuExam b on a.stuNo = b.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 stuSex='女'and stuage>all(SELECT stuage from stuinfo where stuSex='男'); + +-- 14.查询出只是比某个男生年龄大的女生的信息 + +-- +SELECT *from stuinfo where stuSex='女'and stuage>any(SELECT stuage from stuinfo where stuSex='男'); +``` + +------ + +# 笔记 + +```mysql +-- 嵌套在其它SQL语句中的select语句 +-- 子查询通常在外层语句之前优先执行,所以要用()包括起来 +-- 子查询的结果,一般作为外层语句的条件,数据源等 + +select ① from ③ where ②; +形式一,select后面嵌套子查询 +形式二,在where/having 后面做条件 +形式三,在from后面,形成一个临时表,必须加一个别名 + 子查询的结果: + 单列单个值:可以放在select 后,也可以放where/having之后 + 单列多个值:where后,或having后 ,不用直接用<,=,>= 这种单纯的比较运算符。但可以用搭配any(任意一个),all(所有的)等关键字一起使用。还可以用in,not in这种表达式 + 多列时,只能当临时表来表用,放在from后面,而且必须,给他取个别名 +``` + diff --git "a/31 \351\231\210\347\201\265\351\222\260/20230308 \345\244\247\344\275\234\344\270\232.md" "b/31 \351\231\210\347\201\265\351\222\260/20230308 \345\244\247\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..6565ecd56036da3df11e617f5da8a0cb4d1fe2a5 --- /dev/null +++ "b/31 \351\231\210\347\201\265\351\222\260/20230308 \345\244\247\344\275\234\344\270\232.md" @@ -0,0 +1,100 @@ +# 大作业 + +-- 1. 根据前面提供的表结构和表数据,创建数据库并分别创建这张四张表;并插入相关数据。(提醒:外键请注意建表顺序和插入数据的顺序) (30分) + +```mysql +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'等级编号' , +foreign key (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'作家编号' , +foreign key (author_id) references author(author_id), +type_id varchar(20) comment'类型编号', +foreign key (type_id) references type(type_id), +story_name varchar(50) comment'作品名称', +views_number int comment'浏览量' +); +create table type( +type_id varchar(20) comment'类型编号' primary key , +type_name varchar(20) comment'类型名称' not null unique key +); + +insert into vip values +('VIP01','青铜作家'), +('VIP02','白银作家'), +('VIP03','黄金作家'), +('VIP04','钻石作家'); + +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 type values +('L01','玄幻'), +('L02','奇幻'), +('L03','武侠'), +('L04','仙侠'), +('L05','都市') + +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); +``` + +```mysql +-- 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,'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 AUTHOR.* from story inner join author on author.author_id=story.author_id where views_number>1000 and vip_id > 'VIP03' ; +-- 10. 查询姓名以杜字开头的作家的姓名,积分和等级编号。(3分) +select author_name,views_number,author.vip_id from author inner join story on author.author_id=story.author_id where author_name like '杜%'; +-- 11. 查询积分在100、1000之间的作家信息,以积分降序排列。 (3分) + +-- 12. 查询出小说的总浏览量,最高浏览量,最小浏览量,平均浏览量,给字段用上中文别名。(3分) +select sum(views_number) 总浏览量,max(views_number) 最高浏览量 , min(views_number) 最小浏览量 ,avg(views_number ) 平均浏览量 from story; +-- 13. 查询各种等级的作家的平均积分和作家数量,并对查询结果使用中文别名。(3分) +-- 14. 查询小说数量大于等于2的分类编号和小说数量。(4分) + +-- 15. 查询所有小说中浏览量最少的书的作品编号、作品名称和类型编号、浏览量。(4分) +-- 16. 查询积分比刘产高的作者所有信息。(5分) +-- 17. 查询出哪些白银作家是没有写小说的,显示这些作家的姓名、等级名称。(8分) +-- 18. 找出写过作品浏览量大于5000的作家的所有作品中浏览量不到1000的作品信息(8分) +-- 19. 查询所有小说的小说编号、小说名称、浏览量、分类名称、作者姓名、作者积分、作者等级名称,结果字段要用中文别名,并按浏览量降序排列,如果浏览量一样的,再按积分降序排列。(10分) +``` + diff --git "a/31 \351\231\210\347\201\265\351\222\260/20230309 \347\273\203\344\271\240\344\275\234\344\270\232.md" "b/31 \351\231\210\347\201\265\351\222\260/20230309 \347\273\203\344\271\240\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..cb231b286664e93dede276a80623a7e98726df41 --- /dev/null +++ "b/31 \351\231\210\347\201\265\351\222\260/20230309 \347\273\203\344\271\240\344\275\234\344\270\232.md" @@ -0,0 +1,168 @@ +# 作业 + +```mysql +create database xuesen charset utf8; + +use xuesen; + +create table stuinfo ( +stuNO varchar(100), +stuName varchar(100), +stuAge varchar(100), +stuAddress varchar(100), +stuSeat varchar(100), +stuSex varchar(100) +); + +create table stuexam( +examNO varchar(100), +stuNO varchar(100), +writtenExam varchar(100), +labExam varchar(100) +); + +insert into stuinfo values +('s2501','张秋利','20','美国硅谷','1','1'), +('s2502','李斯文','18','湖北武汉','2','0'), +('s2503','马文才','22','湖南长沙','3','1'), +('s2504','欧阳俊雄','21','湖北武汉','4','0'), +('s2505','梅超风','20','湖北武汉','5','1'), +('s2506','陈旋风','19','美国硅谷','6','1'), +('s2507','陈风','20','美国硅谷','7','0'); + +insert into stuexam values +('1','s2501','50','70'), +('2','s2502','60','65'), +('3','s2503','86','85'), +('4','s2504','40','80'), +('5','s2505','70','90'), +('6','s2506','85','90'); + +-- 按图片所给的数据进行数据表的建立和数据插入,然后进行以下查询操作 +-- +-- 1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 + +select stuNO 学号,stuName 名字, stuAge 年龄,stuAddress 地址,stuSeat 座位号,stuSex 性别 from stuinfo; + +-- +-- 2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +-- + +select stuName,stuAge,stuAddress from stuinfo; + +-- 3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 +-- + +select stuNO 学号,writtenExam 笔试分,labExam 机试分 from stuexam; + +-- 5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分这四列的信息 + +select stuinfo.stuNO,writtenexam,labexam,(writtenexam + labexam) aug from stuinfo left join stuexam on stuinfo.stuNO=stuexam.stuNO; + +-- +-- 6.查询学生信息表(stuInfo)中学生来自哪几个地方 + +select stuAddress from stuinfo GROUP BY stuAddress; + +-- +-- 7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名 + +select stuAge 年龄 from stuinfo GROUP BY stuage; + +-- +-- 8.查询学生信息表(stuInfo)中前3行记录 + +select * from stuinfo LIMIT 3; + +-- +-- 9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 + +select stuname,stuseat from stuinfo limit 4; + +-- +-- 11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 + +select * from stuinfo where stuaddress='湖北武汉' and stuage = '20'; + +-- +-- 12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列 + +select labexam from stuexam where labexam between 60 and 80 order by labexam desc; + +-- +-- 13.查询来自湖北武汉或者湖南长沙的学生的所有信息 + +select * from stuinfo where stuaddress='湖北武汉' or stuaddress='湖南长沙'; + +-- +-- 14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列 + + + +-- +-- 15.查询年龄没有写的学生所有信息 + +select * from stuinfo where stuage is null; + +-- +-- 16.查询年龄写了的学生所有信息 + +select * from stuinfo where stuage is not null; + +-- +-- 17.查询姓张的学生信息 + +select * from stuinfo where stuname like '张%'; + +-- +-- 18.查询学生地址中有‘湖’字的信息 + +select * from stuinfo where stuaddress like '%湖%'; + +-- +-- 19.查询姓张但名为一个字的学生信息 + +select * from stuinfo where stuname like '张_'; + +-- +-- 20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +-- + +select * from stuinfo where stuname like '__俊%'; + +-- 21.按学生的年龄降序显示所有学生信息 + +select * from stuinfo order by stuage desc; + +-- +-- 22.按学生的年龄降序和座位号升序来显示所有学生的信息 + +select * from stuinfo order by stuage desc,stuseat; + +-- +-- 23显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 + +select * from stuexam where writtenexam = (select max(writtenexam) from stuexam); + + + +-- +-- 24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 + + + +-- +-- 25.查询每个地方的学生的平均年龄 + +select stuaddress,avg(stuage) from stuinfo GROUP BY stuaddress; + +-- +-- 26.查询男女生的分别的年龄总和 + +select stusex,sum(stuage) from stuinfo GROUP BY stusex; + +-- +-- 27.查询每个地方的男女生的平均年龄和年龄的总和 + + +``` \ No newline at end of file