From a3d4e57286e955416dd70170a4b40873f3928e87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E5=AE=87=E9=91=AB?= <2366023603@qq.com> Date: Sat, 25 Feb 2023 13:22:36 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zuoye04.md" | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 "06 \351\203\255\345\256\207\351\221\253/zuoye04.md" diff --git "a/06 \351\203\255\345\256\207\351\221\253/zuoye04.md" "b/06 \351\203\255\345\256\207\351\221\253/zuoye04.md" new file mode 100644 index 0000000..6c602ab --- /dev/null +++ "b/06 \351\203\255\345\256\207\351\221\253/zuoye04.md" @@ -0,0 +1,138 @@ +## 第1题:员工表 + +```mysql +drop table if exists `employee`; +#创建employee表 +CREATE TABLE employee( + id INT, + `name` VARCHAR(20), + sex VARCHAR(20), + tel VARCHAR(20), + addr VARCHAR(50), + salary FLOAT +); + +#添加信息 +INSERT INTO employee(id,`name`,sex,tel,addr,salary)VALUES +(10001,'张一一','男','13456789000','广东韶关',10010.58), +(10002,'刘小红','女','13454319000','广东江门',12010.21), +(10003,'李四','男','0751-1234567','广东佛山',10040.11), +(10004,'刘小强','男','0755-5555555','广东深圳',15010.23), +(10005,'王艳','男',NULL,'广东广州',14050.16); +``` + +| **id** | **name** | **sex** | **tel** | **addr** | **salary** | +| ------ | -------- | ------- | ------------ | -------- | ---------- | +| 10001 | 张一一 | 男 | 13456789000 | 广东韶关 | 10010.58 | +| 10002 | 刘小红 | 女 | 13454319000 | 广东江门 | 12010.21 | +| 10003 | 李四 | 男 | 0751-1234567 | 广东佛山 | 10040.11 | +| 10004 | 刘小强 | 男 | 0755-5555555 | 广东深圳 | 15010.23 | +| 10005 | 王艳 | 女 | NULL | 广东广州 | 14050.16 | + +**要求1:**查询出薪资在12000~13000之间的员工信息。 + +**要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 + +**要求3:**将“李四”的家庭住址改为“广东韶关” + +**要求4:**查询出名字中带“小”的员工 + +**要求5:**查询出薪资高于11000的男员工信息 + +**要求6:**查询没有登记电话号码的员工 + +**要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 + +**要求8:**查询每个员工的年薪,显示“姓名、年薪” + +```mysql +select * from employee where salary between 12000 and 13000; + +select id,name,addr from employee where name like '%刘%'; + +update employee set addr='广东韶关' where name='李四'; + +select name from employee where name like '%小%'; + +select * from employee where salary>11000 and sex='男'; + +select * from employee where tel is null ; + +select * from employee where salary>12000 or addr ='广东深圳'or'广东广州'; + +select name,salary from employee ; +``` + +## 第2题:国家信息表 + +countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 + +```mysql +DROP TABLE IF EXISTS `countries_info`; +CREATE TABLE `countries_info`( + `name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT +); + +INSERT INTO countries_info VALUES +('Afghanistan','Asia',652230,25500100,20343000000), +('Albania','Europe',28748,2831741,12960000000), +('Algeria','Africa',2381741,37100000,188681000000), +('Andorra','Europe',468,78115,3712000000), +('Angola','Africa',1246700,20609294,100990000000); +``` + +表数据样例: + +```mysql ++-------------+-----------+---------+------------+--------------+ +| name | continent | area | population | gdp | ++-------------+-----------+---------+------------+--------------+ +| Afghanistan | Asia | 652230 | 25500100 | 20343000000 | +| Albania | Europe | 28748 | 2831741 | 12960000000 | +| Algeria | Africa | 2381741 | 37100000 | 188681000000 | +| Andorra | Europe | 468 | 78115 | 3712000000 | +| Angola | Africa | 1246700 | 20609294 | 100990000000 | ++-------------+-----------+---------+------------+--------------+ +``` + +**要求1:** 查询大国 的国家名称、人口和面积。 + +如果一个国家满足下述两个条件之一,则认为该国是 大国 : + +- 面积至少为 300万平方公里(即,3000000 km2) + +- 人口至少为 2500 万(即 25000000) + +**要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 + +**要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 + +**要求4:**查询国家名字中包含“o“字母的国家信息 + +**要求5:**查询GDP值超过10000000000的国家信息 + +**要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” + +**要求7:**查询人均贡献GDP值低于1000的国家信息。 + +**要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” + +```mysql +select * from countries_info where continent = 'Asia'; + +select * from countries_info where area <=10000 and population<=100000; + +select * from countries_info where name like '%o%'; + +select * from countries_info where gdp >= 10000000000; + +select name,population,gdp,(gdp/countries_info.population)as '人均贡献值' from countries_info; + +select * from countries_info where (gdp/countries_info.population)<1000; + +select name,area,population,(area/countries_info.population) from countries_info; +``` -- Gitee From 6cfe4d5800020c42b901142fc4fb751235bcfef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E5=AE=87=E9=91=AB?= <2366023603@qq.com> Date: Sun, 5 Mar 2023 12:58:23 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...24\346\254\241\344\275\234\344\270\232.md" | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 "06 \351\203\255\345\256\207\351\221\253/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" diff --git "a/06 \351\203\255\345\256\207\351\221\253/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" "b/06 \351\203\255\345\256\207\351\221\253/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..04ebfec --- /dev/null +++ "b/06 \351\203\255\345\256\207\351\221\253/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,163 @@ +```sql +create database zy charset utf8; +use zy; +create table student +( + Sno varchar(20) primary key, + Sname varchar(20) not null, + Ssex varchar(20) not null, + Sbirthday datetime, + Class varchar(20) +); +create table course +( + Cno varchar(20) primary key, + Cname varchar(20) not null, + Tno varchar(20) not null +); +create table score +( + Sno varchar(20) not null, + Cno varchar(20) not null, + Degree Decimal(4, 1) not null, + primary key (Sno, Cno) +); + +create table teacher +( + Tno varchar(20) primary key, + Tname varchar(20) not null, + Tsex varchar(20) not null, + Tbirthday datetime, + Prof varchar(20), + Depart varchar(20) not null +); + +insert into student +values (108, '曾华', '男', '1977-9-1', '95033'), + (105, '匡明', '男', '1975-10-2', '95031'), + (107, '王丽', '女', '1976-1-23', '95033'), + (101, '李军', '男', '1976-2-20', '95033'), + (109, '王芳', '女', '1975-2-10', '95031'), + (103, '陆君', '男', '1974-6-3', '95031'); +select * +from student; + +insert into course +values ('3-105', '计算机导论', 825), + ('3-245', '操作系统', 804), + ('6-166', '数字电路', 856), + ('9-888', '高等数学', 831); +select * +from course; + +insert into score +values ('103', '3-245', 86), + ('105', '3-245', 75), + ('109', '3-245', 68), + ('103', '3-105', 92), + ('105', '3-105', 88), + ('109', '3-105', 76), + ('101', '3-105', 64), + ('107', '3-105', 91), + ('108', '3-105', 78), + ('101', '6-166', 85), + ('107', '6-166', 79), + ('108', '6-166', 81); +select * +from score; + +insert into teacher +values ('804', '李诚', '男', '1958-12-2', '副教授', '计算机系'), + ('856', '张旭', '男', '1969-3-12', '讲师', '电子工程系'), + ('825', '王萍', '女', '1972-5-5', '助教', '计算机系'), + ('831', '刘冰', '女', '1977-8-14', '助教', '电子工程系'); +select * +from teacher; + +alter table course + add foreign key (Tno) references teacher (Tno); +alter table score + add foreign key (Sno) references student (Sno); +alter table score + add foreign key (Cno) references course (Cno); + + +-- 查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ +select st.Sname,co.Cname from student st,course co,score sc where st.Sno=sc.Sno and co.Cno=sc.Cno; + +-- 查询没有学生的教师的所有信息 +select * from teacher te left join course co on te.Tno = co.Tno left join score sc on co.Cno = sc.Cno where sc.Sno is null ; + +-- 查询Score表中的最高分的学生学号和课程号。 +select Sno,Cno from score where Degree=(select max(Degree) from score); + +-- 查询所有学生的Sname、Cno和Degree列。 +select st.Sname,sc.Cno,sc.Degree from student st ,score sc where st.sno=sc.sno; + +-- 查询所有学生的Sno、Cname和Degree列。 +select st.Sno,st.Sname,sc.Degree from student st ,score sc where st.sno=sc.sno; + +-- 查询所有学生的Sname、Cname和Degree列。 +select st.Sname,co.Cname,sc.Degree from student st ,course co ,score sc where st.sno=sc.sno and co.Cno=sc.Cno; + +-- 查询“95033”班学生的平均分。 +select avg(sc.Degree) from student st, score sc where st.Sno=sc.Sno and Class='95033'; + +-- 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select * from score where Degree>(select Degree from score where Sno='109'and Cno='3-105') and Cno='3-105'; + +-- 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +select * from score where Degree not in (select max(Degree) from score group by Cno); + +-- 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * from score where Degree>(select Degree from score where Sno='109' and Cno='3-105'); + +-- 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +select Sno,Sname,Sbirthday from student where year(Sbirthday)=(select year(Sbirthday) from student where Sno='108') and Sno!='108'; + +-- 查询“张旭“教师任课的学生成绩。 +select Degree from teacher te,score sc,course co where te.Tno= co.Tno and co.Cno=sc.Cno and tname='张旭'; + +-- 查询选修某课程的同学人数多于5人的教师姓名。 +select tname from course co,teacher te where co.Tno=te.Tno and Cno=(select Cno from score group by Cno having count(*)>5); + +-- 查询出“计算机系“教师所教课程的成绩表。 +select sc.* from course co, score sc where co.Cno = sc.Cno and co.Tno in(select Tno from teacher where Depart='计算机系'); + +-- 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select Tname,Prof from teacher where Prof not in (select Prof from teacher where Depart='计算机系') +union all +select Tname,Prof from teacher where Prof not in (select Prof from teacher where Depart='电子工程系'); + +-- 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select Cno,Sno,Degree from score where Cno='3-105' and Degree>all(select Degree from score where Cno='3-245') order by Degree desc; + +-- 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +select * from score where Cno='3-105' and Degree>all(select Degree from score where Cno='3-245'); + +-- 查询成绩比该课程平均成绩低的同学的成绩表。 +select * from score a where a.Degree<(select avg(Degree) from score b where a.Cno=b.Cno); + +-- 查询所有任课教师的Tname和Depart. +select Tname,Depart from teacher; + +-- 查询所有未讲课的教师的Tname和Depart. +select te.tname,te.Depart from teacher te left join course co on te.Tno = co.Tno left join score sc on co.Cno = sc.Cno where sc.Sno is null ; + +-- 查询“男”教师及其所上的课程。 +select * from teacher te,course co where te.tno=co.Tno and Tsex='男'; + +-- 查询最高分同学的Sno、Cno和Degree列。 +select sno,Cno,Degree from score where Cno in(select Cno from score group by Cno) and Degree in (select max(Degree) from score group by Cno); + +-- 查询和“李军”同性别的所有同学的Sname. +select Sname from student where Ssex=(select Ssex from student where Sname='李军'); + +-- 查询和“李军”同性别并同班的同学Sname. +select Sname from student where Ssex=(select Ssex from student where Sname='李军') and Class=(select class from student where Sname='李军'); + +-- 查询所有选修“计算机导论”课程的“男”同学的成绩表。 +select * from student st , score sc ,course co where st.Sno=sc.Sno and co.Cno=sc.Cno and st.Ssex='男' and co.Cname='计算机导论'; + +``` -- Gitee From 167f5f30f9d9bd8bf79e09df1e5609cf2f2a325f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E5=AE=87=E9=91=AB?= <2366023603@qq.com> Date: Wed, 8 Mar 2023 00:09:28 +0800 Subject: [PATCH 3/3] =?UTF-8?q?20230306=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230306 \344\275\234\344\270\232.md" | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 "06 \351\203\255\345\256\207\351\221\253/20230306 \344\275\234\344\270\232.md" diff --git "a/06 \351\203\255\345\256\207\351\221\253/20230306 \344\275\234\344\270\232.md" "b/06 \351\203\255\345\256\207\351\221\253/20230306 \344\275\234\344\270\232.md" new file mode 100644 index 0000000..5b70f8b --- /dev/null +++ "b/06 \351\203\255\345\256\207\351\221\253/20230306 \344\275\234\344\270\232.md" @@ -0,0 +1,103 @@ +```mysql +CREATE DATABASE text charset utf8; +use text; +CREATE table stuinfo( +stuNO VARCHAR(10) primary key, +stuName VARCHAR(10), +stuSex VARCHAR(10), +stuAge VARCHAR(10), +stuAddress VARCHAR(20), +stuSeat INT +); +CREATE table stuExam( +examNo int primary key auto_increment, +stuNO VARCHAR(10), +writtenExam int, +labExam INT +); +CREATE table stuMarks( +examNo int primary key auto_increment, +stuID VARCHAR(10), +score INT +); +alter table stuExam add foreign key (stuNO) references stuinfo(stuNO); +alter table stuMarks add foreign key (examNo) references stuExam(examNo); + + +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 * from stuinfo where stuAge>(select avg(stuAge) from stuinfo); + +# 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select a.stuNO,a.stuName,a.stuSex,max(score) from stuinfo a left join stumarks b on a.stuNO=b.stuID group by stuNO; + +# 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select a.stuNO,a.stuName,a.stuSex,(b.writtenExam+b.labExam)/2 平均分 from stuinfo a left join stuexam b on a.stuNO=b.stuNO; + +# 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuSex='男' and stuAge>=20; + +# 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stuSex='女' and stuAge>(select max(stuAge) from stuinfo where stuSex='男'); + +# 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select a.* from stuinfo a, (select stuID from stumarks group by stuID having min(score)>=60) b where a.stuNO=b.stuID; + +# 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.* from stuinfo a,(select stuID from stumarks group by stuID) b where a.stuNO=b.stuID; + +# 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select * from stuinfo where stuNO not in(select stuID from stumarks group by stuID); + +# 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select a.* from stuinfo a,(select stuID from stumarks group by stuID having max(score)>90) b where a.stuNO=b.stuID; + +# 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select a.* from stuinfo a,(select stuID from stumarks group by stuID having avg(score)>80) b where a.stuNO=b.stuID; + +# 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select a.* from +stuinfo a, +(select stuID from stumarks where stuID!='s2501' group by stuID having max(score)>(select max(score) from stumarks where stuID='s2501')) b +where + a.stuNO=b.stuID; + +# 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select a.* from +stuinfo a, +(select stuID from stumarks where stuID!='s2501' group by stuID having max(score)>(select min(score) from stumarks where stuID='s2501')) b +where + a.stuNO=b.stuID; + +# 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stuinfo where stuSex='女' and stuAge>(select max(stuAge) from stuinfo where stuSex='男'); + +# 14.查询出只是比某个男生年龄大的女生的信息 +select * from stuinfo where stuSex='女' and stuAge>(select min(stuAge) from stuinfo where stuSex='男'); + + +``` -- Gitee