From a1c9eae729ff41f83c051d5aedd979f8a076c7f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98=E6=9D=A8=E6=AF=85=E2=80=99?= <‘y233emmm@163.com’> Date: Tue, 7 Mar 2023 22:20:41 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9D=A8=E6=AF=85=E7=9A=84=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\350\241\250\346\237\245\350\257\242.md" | 2 +- ...6 \345\255\220\346\237\245\350\257\242.md" | 128 ++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 "32 \346\235\250\346\257\205/20230306 \345\255\220\346\237\245\350\257\242.md" diff --git "a/32 \346\235\250\346\257\205/20230227 \345\244\232\350\241\250\346\237\245\350\257\242.md" "b/32 \346\235\250\346\257\205/20230227 \345\244\232\350\241\250\346\237\245\350\257\242.md" index 4e03bba..c795cd5 100644 --- "a/32 \346\235\250\346\257\205/20230227 \345\244\232\350\241\250\346\237\245\350\257\242.md" +++ "b/32 \346\235\250\346\257\205/20230227 \345\244\232\350\241\250\346\237\245\350\257\242.md" @@ -176,7 +176,7 @@ INSERT INTO Teacher VALUES 查询条件;Score.Sno = Student.Sno Course.Cno = Score.Cno Teacher.Tno = Course.Tno ① 查询Score表中的最高分的学生学号和课程号。 -SELECT a.Sno,a.Cno from Score a WHERE (SELECT max(Degree) from Score b); +SELECT a.Sno,a.Cno from Score a WHERE Degree = (SELECT max(Degree) from Score ); ② 查询所有学生的Sname、Cno和Degree列。 SELECT Sname,Cno,Degree from Student join Score on Student.Sno = Score.Sno; diff --git "a/32 \346\235\250\346\257\205/20230306 \345\255\220\346\237\245\350\257\242.md" "b/32 \346\235\250\346\257\205/20230306 \345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..935c8f5 --- /dev/null +++ "b/32 \346\235\250\346\257\205/20230306 \345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,128 @@ +```mysql +CREATE database class0306 charset utf8; +use class0306; +CREATE TABLE stuinfo( + stuno VARCHAR(5) primary key, + stuname VARCHAR(4), + stusex char, + stuage int, + stuaddress VARCHAR(4), + stuseat int + ); + +CREATE TABLE stuexam( + examno int PRIMARY key, + stuno VARCHAR(5), + writtenexam int , + labexam 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 avg(stuage) from stuinfo + +SELECT * from stuinfo where stuage > (SELECT avg(stuage) from stuinfo); +-- + +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +SELECT max(score) from stumarks GROUP BY stuid + +SELECT stuno,stuname,stusex,score from stuinfo left join stumarks on stumarks.stuid=stuinfo.stuno where score in (SELECT max(score) from stumarks GROUP BY stuid); +-- + +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) + +SELECT a.stuno,a.stuname,a.stusex,(writtenexam+labexam)/2 from stuinfo a join stuexam b on a.stuno=b.stuno GROUP BY stuno; +-- + +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +SELECT * from stuinfo where stusex = '男' and stuage >= 20; +SELECT stuage from stuinfo where stusex = '男' +SELECT * from stuinfo where stusex in (SELECT stusex from stuinfo where stusex = '男') and stuage in (SELECT stuage from stuinfo where stuage >= 20); +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +SELECT stuage from stuinfo where stusex = '男' +SELECT * from stuinfo where stuage >any (SELECT stuage from stuinfo where stusex = '男') and stusex = '女'; +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) + +SELECT * from stuinfo LEFT JOIN stumarks on stumarks.stuid=stuinfo.stuno where stuid != (SELECT stuid from stumarks where score < 60); +-- + +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +SELECT stuinfo.* from stuinfo join stumarks on stuinfo.stuno = stumarks.stuid; + +SELECT stuinfo.* FROM stuinfo where stuno in (SELECT stuid FROM stumarks); +-- + +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +SELECT * FROM stuinfo WHERE stuno not in (SELECT DISTINCT stuno from stuinfo right join stumarks on stuinfo.stuno = stumarks.stuid); + +SELECT stuinfo.* FROM stuinfo where stuno not in (SELECT stuid FROM stumarks); +-- + +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) + +SELECT stuinfo.* from stuinfo join stumarks on stuinfo.stuno = stumarks.stuid where score in (SELECT score FROM stumarks where score > 90) +-- + +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +SELECT stuinfo.* FROM stuinfo JOIN stumarks on stuinfo.stuno = stumarks.stuid where score in (SELECT avg(score) from stumarks GROUP BY stuid HAVING avg(score) > 80 ); + +SELECT avg(score) from stumarks GROUP BY stuid HAVING avg(score) > 80 +-- + +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +SELECT score from stumarks,stuinfo where stuinfo.stuno = stumarks.stuid and stuname = '张秋利' + +SELECT * from stumarks,stuinfo where stuinfo.stuno = stumarks.stuid and score >all (SELECT score from stumarks,stuinfo where stuinfo.stuno = stumarks.stuid and stuname = '张秋利'); +-- + +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) + +SELECT * from stumarks,stuinfo where stuinfo.stuno = stumarks.stuid and score >any (SELECT score from stumarks,stuinfo where stuinfo.stuno = stumarks.stuid and stuname = '张秋利') and stuname != '张秋利'; +-- + +-- 13.查询班上比所有男生年龄都要大的女生的信息 +SELECT max(stuage) from stuinfo where stusex = '男' + +SELECT * from stuinfo WHERE stuage > (SELECT max(stuage) from stuinfo where stusex = '男') and stusex = '女'; +-- + +-- 14.查询出只是比某个男生年龄大的女生的信息 +SELECT * from stuinfo WHERE stuage >any (SELECT stuage from stuinfo where stusex = '男') and stusex = '女'; +``` + -- Gitee From 7063308401723cc24aca6f5430da806f61c37c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98=E6=9D=A8=E6=AF=85=E2=80=99?= <‘y233emmm@163.com’> Date: Thu, 9 Mar 2023 00:43:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9D=A8=E6=AF=85=E7=9A=84=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\350\200\203\350\257\225\351\242\230.md" | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 "32 \346\235\250\346\257\205/20230308 \345\216\273\345\271\264\350\200\203\350\257\225\351\242\230.md" diff --git "a/32 \346\235\250\346\257\205/20230308 \345\216\273\345\271\264\350\200\203\350\257\225\351\242\230.md" "b/32 \346\235\250\346\257\205/20230308 \345\216\273\345\271\264\350\200\203\350\257\225\351\242\230.md" new file mode 100644 index 0000000..6a0e949 --- /dev/null +++ "b/32 \346\235\250\346\257\205/20230308 \345\216\273\345\271\264\350\200\203\350\257\225\351\242\230.md" @@ -0,0 +1,139 @@ +```mysql +CREATE DATABASE xiaoshuo CHARSET utf8; +use xiaoshuo; + + +-- 奶奶滴,漏写自增,删表跑路 +-- DROP TABLE author; +-- DROP TABLE vip; +-- DROP TABLE story; + +-- DROP TABLE type; +-- + +CREATE TABLE author( + author_id int PRIMARY key COMMENT '作家编号', + author_name varchar(20) not null UNIQUE COMMENT '作家姓名', + credits int COMMENT '积分', + vip_id varchar(20) not null COMMENT '等级编号', + FOREIGN key (vip_id) REFERENCES vip(vip_id) on UPDATE CASCADE + ) COMMENT '作家信息表'; + + + CREATE TABLE vip( + vip_id varchar(20) PRIMARY key COMMENT '等级编号', + vip_name varchar(20) not null UNIQUE COMMENT '等级名称' + ) 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) +​ ) COMMENT '小说作品信息表'; +​ + + CREATE TABLE type( + type_id varchar(20) PRIMARY key COMMENT '类型编号', + type_name varchar(20) not null unique COMMENT '类型名称' + ) 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','都市'); + + + + + +-- # 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'男'; +desc author; +-- 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 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 ('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 avg(credits) '平均积分',count(*) '作家数量' FROM author GROUP BY vip_id; +-- 14. 查询小说数量大于等于2的分类编号和小说数量。(4分) +SELECT COUNT(type_id),type_id FROM story GROUP BY type_id; +-- 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 a.author_id FROM author a WHERE author_id not in (SELECT b.author_id FROM story b) and author_id in (SELECT a.author_id FROM author a join vip on vip.vip_id = a.vip_id WHERE vip_name = '白银作家') +-- 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 a.story_id '小说编号',a.story_name '小说名称',a.views_number '浏览量',b.type_name '分类名称',c.author_name '作者姓名',c.credits '作者积分',d.vip_name '作者等级' FROM story a,type b,author c,vip d WHERE d.vip_id = c.vip_id and a.author_id = c.author_id and b.type_id = a.type_id; +``` + -- Gitee