From 5799f0fb3ef48c9e0673d078f0a2ef6a8b0eb1ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=81=E6=B6=9B?= <1841582040@qq.com> Date: Fri, 24 Feb 2023 07:53:02 +0800 Subject: [PATCH 1/3] =?UTF-8?q?mysql=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...16\350\277\220\347\256\227\347\254\246.md" | 290 ++++++++++++++++++ 1 file changed, 290 insertions(+) create mode 100644 "57 \351\273\204\346\265\201\346\266\233/20230223 DQL\344\270\216\350\277\220\347\256\227\347\254\246.md" diff --git "a/57 \351\273\204\346\265\201\346\266\233/20230223 DQL\344\270\216\350\277\220\347\256\227\347\254\246.md" "b/57 \351\273\204\346\265\201\346\266\233/20230223 DQL\344\270\216\350\277\220\347\256\227\347\254\246.md" new file mode 100644 index 0000000..6d8d963 --- /dev/null +++ "b/57 \351\273\204\346\265\201\346\266\233/20230223 DQL\344\270\216\350\277\220\347\256\227\347\254\246.md" @@ -0,0 +1,290 @@ +# 笔记 + +## DQL + +select语句的基本语法: select 常量; select 表达式; select 函数; + +例:select 1; select 9/2; select NOW(); + +1.从数据中筛选数据的格式: select 字段列表 from 表名称;【where 条件;】 + +2.使用别名: + + 格式:select 字段名 【as】别名 from 表名称 【as】 别名; + + --列的别名有空格时,加双引号。没有时,可加可不加。 + + --表的别名不能加双引号,表的别名中不加空格。 + +3.结果去重: distinct + + 格式: select distinct 字段列表 from 表名称 【where 条件】; + + --distinct只能发在第一个字段前面 --它只支持一个字段。 + +## 运算符 + +1.算数运算符: + +加 + ,减 - , 乘 * ,除 / 或div , (div只保留整数部分) , 模(余) % ,mod + +2.比较运算符: + +大于 > ,小于 < , 大于等于 >= , 小于等于 <= , 等于 = , 不等于 != 。 (等于与不等于不能用于null计算) + +3.区间运算符: + + 区间范围: between x and y not between x and y + + 集合范围: in(x,y,z) not in (x,y,z) # between....and ......(结果包含两边边界) + +4.模糊比较运算符: + + %:代表任意个字符 _:代表一个字符,几个下划线就代表几个字符。 + + 格式:select * from 表名 where 字段名 ' '; + + #查询当前mysql数据库在字符集情况:show variables like ‘%character%’ + +5.逻辑运算符: + + 与: && 或 and + + 或: || 或or + + 非: !或者not + + 异或:xor + +6.关于null值的问题: + +​ 1.判断时: xx is null xx is not null xx<=>null + +​ 2.计算时: if null (xx,代替值) 当xx是null时,用替代值算。 + + + +# 课堂作业 + +## 作业1 + +``` mysql +drop table if exists user_profile; +create database zy charset utf8; +use zy; +CREATE TABLE `user_profile` ( +`id` int, +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`province` varchar(32) +); +INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai'); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang'); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong'); +-- (1)题目:从用户信息表中取出学校的去重数据 +SELECT university FROM user_profile; +select distinct `university` from user_profile; +-- (2)题目:查看用户明细设备ID数据,并将列名显示为 'user_infos_example' +select device_id as user_infos_example from user_profile; +-- (3)题目:查询university是北京大学的设备ID +select device_id,university from user_profile where university = '北京大学'; +-- (4)题目:查询年龄大于24用户的设备ID、性别、年龄、学校 +select device_id,gender,age,university from user_profile where age >24; +-- (5)题目:查询所有用户的设备id、性别、年龄、学校 +select device_id,gender,age,university from user_profile; +-- (6)题目:查询所有用户的数据 +select * from user_profile; +-- (7)题目:查询省份是"shanghai"的用户信息 +select * from user_profile where province = 'Shanghai'; +-- (8)题目:查询所有男性用户的设备ID、年龄、学校 +select device_id,age,university from user_profile where gender = 'male'; +-- (9)题目:从用户信息表中取出省份的去重数据 +SELECT province FROM user_profile; +select distinct province from user_profile; +``` + +## 作业2 + + #### 作业1 + +``` MYSQL +drop table if exists user_profile; +create database zy2 charset utf8; +use zy2; +CREATE TABLE `user_profile` ( +`id` int, +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`province` varchar(32)); + +INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai'); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang'); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong'); + +-- (1)题目:查询年龄20岁及以上且23岁及以下用户的设备ID、性别、年龄 +select device_id,gender,age from user_profile where age>=20 and age<=23; +-- (2)题目:查询除复旦大学以外的所有用户的设备ID、性别、年龄、大学 +select device_id,gender,age,university from user_profile where university != '复旦大学'; +-- (3)题目:查询年龄不为空的用户的设备ID,性别,年龄,学校的信息 +select device_id,gender,age,university from user_profile where age is not null; +``` + +#### 作业2 + +``` mysql +drop table if exists user_profile; +create database zy3 charset utf8; +use zy3; +CREATE TABLE `user_profile` ( +`id` int , +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`gpa` float, +`active_days_within_30` float, +`question_cnt` float, +`answer_cnt` float +); + +INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70); +INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3,15,7,13); +-- (4)题目:查询男性且GPA在3.5以上(不包括3.5)的用户的设备ID,性别、年龄、学校、gpa +select device_id,gender,age,university,gpa from user_profile where gpa>=3.5 and age is not null; +-- (5)题目:查询学校为北大或GPA在3.7以上(不包括3.7)的用户的设备ID,性别、年龄、学校、gpa +select device_id,gender,age,university,gpa from user_profile where university ='北京大学' or gpa>3.7; +-- (6)题目:查询学校为北大、复旦和山大用户的设备ID,性别、年龄、学校、gpa +select device_id,gender,age,university,gpa from user_profile where university ='北京大学' or university ='复旦大学' or university ='山东大学'; +-- (7)题目:查询gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学 +select device_id,gender,age,university,gpa from user_profile where gpa>3.5 and university ='山东大学' or gpa>3.8 and university ='复旦大学'; +-- (8)题目:所有大学中带有北京的用户信息 +select device_id,age,university from user_profile where university like '%北京%'; +``` + + + + + +# 课后作业 + + ### 作业1 + +``` mysql +-- ## 第1题:员工表 +create database zy1 charset utf8; +use zy1; + +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); +-- +select * from employee; +-- +-- **要求1:**查询出薪资在12000~13000之间的员工信息。 +select * from employee where salary between 12000 AND 13000; +-- +-- **要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 +select id,`name`,addr from employee where `name` like '%刘%'; +-- +-- **要求3:**将“李四”的家庭住址改为“广东韶关” +update employee set addr = '广东韶关' where name ='李四'; +-- +-- **要求4:**查询出名字中带“小”的员工 +select * from employee where `name` like '%小%'; +-- +-- **要求5:**查询出薪资高于11000的男员工信息 +select * from employee where sex = '男' and salary >11000; +-- +-- **要求6:**查询没有登记电话号码的员工 +select * from employee where tel is null; +-- +-- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 +select * from employee where sex = '男' and (salary >12000 or (addr = '广东深圳' and addr = '广东广州')); +-- +-- **要求8:**查询每个员工的年薪,显示“姓名、年薪” +select `name`,salary from employee; +``` + +#### 作业2 + +``` mysql +-- ## 第2题:国家信息表 +-- +-- countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 +-- +create database zy2 charset utf8; +use zy2; + 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); +-- +-- **要求1:** 查询大国 的国家名称、人口和面积。 +select `name`,population,`area` FROM `countries_info` where `area` >= 3000000 and population >= 25000000; +-- +-- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : +-- +-- - 面积至少为 300万平方公里(即,3000000 km2) +-- +-- - 人口至少为 2500 万(即 25000000) +-- +-- **要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 +select * from `countries_info` where `continent` = 'Asia'; +-- +-- **要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 +select * from `countries_info` where `area`<10000 and population < 100000; +-- +-- **要求4:**查询国家名字中包含“o“字母的国家信息 +select * from `countries_info` where `name` like '%o%'; +-- +-- **要求5:**查询GDP值超过10000000000的国家信息 +select * from `countries_info` where gdp >= 10000000000; +-- +-- **要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” +select `name`,population,gdp,gdp/population '人均gdp' from `countries_info`; +-- +-- **要求7:**查询人均贡献GDP值低于1000的国家信息。 +select * from `countries_info` where gdp/population < 1000; +-- +-- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select `name`,`area`,population,`area`/population '人均国土面积' from `countries_info`; +``` + -- Gitee From 31775f1e67da7789cf99a89b784c33d5167e0e16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=81=E6=B6=9B?= <1841582040@qq.com> Date: Thu, 2 Mar 2023 12:39:01 +0800 Subject: [PATCH 2/3] =?UTF-8?q?mysql=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...63\350\201\224\346\237\245\350\257\242.md" | 179 ++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 "57 \351\273\204\346\265\201\346\266\233/20230301 \345\207\275\346\225\260\344\270\216\345\205\263\350\201\224\346\237\245\350\257\242.md" diff --git "a/57 \351\273\204\346\265\201\346\266\233/20230301 \345\207\275\346\225\260\344\270\216\345\205\263\350\201\224\346\237\245\350\257\242.md" "b/57 \351\273\204\346\265\201\346\266\233/20230301 \345\207\275\346\225\260\344\270\216\345\205\263\350\201\224\346\237\245\350\257\242.md" new file mode 100644 index 0000000..ed42fda --- /dev/null +++ "b/57 \351\273\204\346\265\201\346\266\233/20230301 \345\207\275\346\225\260\344\270\216\345\205\263\350\201\224\346\237\245\350\257\242.md" @@ -0,0 +1,179 @@ +# 笔记 + +## 函数 + + 1.mysql中函数分为两大类。 + + (1)系统预定义的函数,可以直接调用。 + + (2)用户自定义的函数,需要自己声明,然后才能使用。 + +2.系统预定义的函数,又可分为两大类。 + + (1)单行函数 #所谓单行函数是指一行记录通过单行函数计算完结果还是一行 + + (2)多行函数(聚合函数,分组函数) + +​ #所谓多行函数是指一行或者多行记录通过单行函数计算完,结果的记录数会减少。 + +##### 常用聚合函数类型 + +AUG(x):求平均值 + +SUM(x) :求总和 + +MAX (x):求最大值 + +MIN (x):求最小值 + +COUNT (x):统计记录数 + +## 关联查询 (两个或更多个表一起查询) + +​ #前提条件:这些一起查询的表之间是有关系的(一对一或一对多)之间一定有关联字段,关联字段可能建立了外键,也可能没有建立外键。 + + ##### 1.内连接 + + inner join .....on + + 结果:a∩b a表 inner join b表 + +##### 2.左连接 + + A left join B on + + (1)结果:a表全部 + +​ a表 left join b表 + + (2)结果:a表 - a∩b + +​ a表 left join b表 where a表.关联字段 is null + +##### 3.右连接 + + A right join B on + +​ (1)结果:b表全部 + +​ a表 right join b表 + +​ (2)结果:b表 - a∩b + +​ a表 right join b表 where a表.关联字段 is null + +##### 4.全外连接 + +​ 用 union 合并 + +(1) a表∪b表 a表结果 union b表结果 + +​ #查询a结果的sql union 查询b结果的sql + +(2)a∪b - a∩b a表 - a∩b结果 union b表 - a∩b结果 + +​ #查询a - a∩b的sql union 查询b - a∩b的sql + +##### 5.特殊的内连接: + +标准写法:a inner join b on 条件 + +特殊写法:from a,b where 条件 + + + +# 作业 + + + +``` sql +-- 1. 创建数据库与表格 + +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 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 +); +create table course( +cno varchar(20) primary key, +cname varchar(20) not null, +tno varchar(20), foreign key (tno) references teacher (tno) +); +create table score( +sno varchar(20), foreign key (sno) references student(sno), +cno varchar(20), foreign key (cno) references course(cno), +degree decimal(4,1) +); + +-- 2.插入数据 +-- 学生表 +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'); +-- 教师表 +insert into teacher values ('804','李诚','男','1958-12-2','副教授','计算机系'),('856','张旭','男','1969-3-12','讲师','电子工程系'),('825','王萍','女','1972-5-5','助教','计算机系'),('831','刘冰','女','1977-8-14','助教','电子工程系'); +-- 课程表 +insert into course values ('3-105','计算机导论','825'),('3-245','操作系统','804'),('6-166','数字电路','856'),('9-888','高等数学','831'); +-- 成绩表 +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); + +-- 3. 数据库中的数据: + + +-- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ +select * from (student a left join score b on a.sno = b.sno) right join course c on b.cno = c.cno; +-- 2,查询没有学生的教师的所有信息 + +-- 4. 查询 + + +-- 1 查询Score表中的最高分的学生学号和课程号。 +select sno,cno,max(degree) from score; +-- 2 查询所有学生的Sname、Cno和Degree列。 +select student.sname,score.cno,score.degree from +score inner join student on student.sno=score.sno; +-- 3 查询所有学生的Sno、Cname和Degree列。 +select student.sno,course.cno,score.degree from student inner join score inner join course on student.sno=score.sno and score.cno=course.cno; +-- 4 查询所有学生的Sname、Cname和Degree列。 +select student.sname,course.cname,score.degree from student inner join score inner join course on student.sno=score.sno and score.cno=course.cno; +-- 5 查询“95033”班学生的平均分。 +select AVG(score.degree) from score inner join student on score.sno=student.sno where student.class = '95033'; +-- 6 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select student.sname,course.cname,score.sno,score.cno,score.degree from +-- 7 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +-- 8 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +-- 9 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +-- 10 查询“张旭“教师任课的学生成绩。 +-- 11 查询选修某课程的同学人数多于5人的教师姓名。 +-- 12 查询出“计算机系“教师所教课程的成绩表。 +-- 13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +-- 14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +-- 15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +-- 16 查询成绩比该课程平均成绩低的同学的成绩表。 +-- 17 查询所有任课教师的Tname和Depart. +select tname,depart from teacher; +-- 18 查询所有未讲课的教师的Tname和Depart. + +-- 19 查询“男”教师及其所上的课程。 +select teacher.tname,course.cname from teacher inner join course on teacher.tno=course.tno where teacher.tsex = '男'; +-- 20 查询最高分同学的Sno、Cno和Degree列。 +select sno,cno,degree MAX(degree) from score; +-- 21 查询和“李军”同性别的所有同学的Sname. +select sname from student where ssex = '男'; +-- 22 查询和“李军”同性别并同班的同学Sname. +select sname from student where ssex = '男' and class = '95033'; +-- 23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 +select student.sname,score.degree from student inner join score inner join course on student.sno=score.sno and score.cno=course.conon where student.ssex = '男' and course.cname = '计算机导论'; +-- +``` + -- Gitee From fafdd41f48c32c1f7499f4b9ae62aaf48261c257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=81=E6=B6=9B?= <1841582040@qq.com> Date: Tue, 7 Mar 2023 21:43:46 +0800 Subject: [PATCH 3/3] =?UTF-8?q?mysql=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7 \345\255\220\346\237\245\350\257\242.md" | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 "57 \351\273\204\346\265\201\346\266\233/20230307 \345\255\220\346\237\245\350\257\242.md" diff --git "a/57 \351\273\204\346\265\201\346\266\233/20230307 \345\255\220\346\237\245\350\257\242.md" "b/57 \351\273\204\346\265\201\346\266\233/20230307 \345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..df4dd3e --- /dev/null +++ "b/57 \351\273\204\346\265\201\346\266\233/20230307 \345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,134 @@ +# 笔记 + +##### 子查询 + +- 嵌套在其他SQL语句中的查询语句。 +- 子查询通常先运行,所以要用()包裹起来。 +- 子查询的运行结果,可以作为其他语句的条件,数据源等。 + +##### 子查询分三种形式 + +​ select 1 from 2 where 3 + + 1.放在select后面 + + 2.放在where/having后面 + + 3.放在from后面,子查询当做一个临时的表使用,必须要给别名。 + + + +##### where或having后的子查询,其子查询的结果有三种形式 + + 1.单列单个值,可以直接使用比较运算符 + + 2.单列多个值,可以使用 in ,not in 进行比较 + + 3.单列多个值,可以使用比较运算符,搭配any,all,等关键字与查询结果进行比较 + + + +##### 子查询结果是单列,可以放在select参与运算,或者是作为字段,也可以放在where / having后当条件。 + +##### 子查询结果是多列,多个值,只能放在from后面当临时表使用,并且必须取别名。 + +# 作业 + +``` mysql +-- 创建数据库 + +create database 57t charset utf8; + +-- 使用数据库 + +use 57t; + +-- 创建表格 + +create table stuinfo( +stuno varchar(10) primary key, +stuname varchar(10), +stusex char, +stuage int, +stuaddress varchar(10), +stuseat int +); +create table stuexam( +examno int primary key, +stuno varchar(10), +writtenexam int, +labexam int, +foreign key (stuno) references stuinfo(stuno) +); +create table stumarks( +examno int, +stuid varchar(10), +score int, +foreign key (examno) references stuexam(examno), +foreign key (stuid) references stuinfo(stuno) +); + +-- 插入数据 + +insert into stuinfo values ('s2501','张秋利','男',20,'美国硅谷',1),('s2502','李斯文','女',18,'湖北武汉',2),('s2503','马文才','男',18,'湖南长沙',3),('s2504','欧阳俊雄','女',21,'湖北武汉',4),('s2505','梅超风','男',16,'湖北武汉',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,c.`选修课最高分` from stuinfo a right join stumarks b on a.stuno = b.stuid right join (select examno,max(score) 选修课最高分 from stumarks group by stuid) c on b.examno = c.examno; + +-- -- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select a.stuno,stuname,stusex,((b.writtenexam+labexam)/2) 考试平均分 from stuinfo a left join stuexam b on a.stuno = b.stuno; + +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +-- (1) +select * from stuinfo where stusex = '男' and stuage >=20; +-- (2) +select * from stuinfo where stuage = (select stuage from stuinfo where stuage >= 20 and stusex = '男'); + + +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stusex = '女' and stuage = (select max(stuage) from stuinfo); + +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select distinct a.stuno,a.stuname,a.stusex,a.stuage,a.stuaddress,a.stuseat from stuinfo a right join stumarks b on a.stuno = b.stuid where b.score >= 60 ; + +-- +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select distinct * from stuinfo a left join stumarks b on a.stuno = b.stuid where b.score is not null; +-- +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select distinct * from stuinfo a left join stumarks b on a.stuno = b.stuid where b.score is null; +-- +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select a.stuno,a.stuname,a.stusex,a.stuage,a.stuaddress,a.stuseat from stuinfo a left join stuexam b on a.stuno = b.stuno where b.writtenexam >= 90 or b.labexam >=90; + +-- +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select a.stuno,a.stuname,a.stusex,a.stuage,a.stuaddress,a.stuseat,((b.writtenexam + b.labexam)/2) 平均成绩 from stuinfo a left join stuexam b on a.stuno = b.stuno where ((b.writtenexam + b.labexam)/2) > 80; + +-- +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select a.stuno,a.stuname,a.stusex,a.stuage,a.stuaddress,a.stuseat from stuinfo a left join stuexam b on a.stuno = b.stuno where b.writtenexam > (select writtenexam from stuexam where stuno = 's2501') and b.labexam > (select labexam from stuexam where stuno = 's2501'); +-- +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select a.stuno,a.stuname,a.stusex,a.stuage,a.stuaddress,a.stuseat from stuinfo a left join stuexam b on a.stuno = b.stuno where b.writtenexam > (select writtenexam from stuexam where stuno = 's2501') or b.labexam > (select labexam from stuexam where stuno = 's2501'); + +-- +-- 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stuinfo where stusex = '女' and stuage = (select max(stuage) from stuinfo); +-- +-- 14.查询出只要比某个男生年龄大的女生的信息 +select * from stuinfo where stusex = '女' and stuage > (select min(stuage) from stuinfo where stusex = '男'); + +``` + + + -- Gitee