From e82aba0d0724fe4d4bad636af0fc6c3c83caad36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E5=98=89=E4=B9=90?= <3196825236@qq.com> Date: Thu, 23 Feb 2023 21:35:15 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=AC=AC=E4=B8=89=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 --- ...20\347\256\227\347\254\224\350\256\260.md" | 349 ++++++++++++++++++ 1 file changed, 349 insertions(+) create mode 100644 "13 \350\224\241\345\230\211\344\271\220/\350\277\220\347\256\227\347\254\224\350\256\260.md" diff --git "a/13 \350\224\241\345\230\211\344\271\220/\350\277\220\347\256\227\347\254\224\350\256\260.md" "b/13 \350\224\241\345\230\211\344\271\220/\350\277\220\347\256\227\347\254\224\350\256\260.md" new file mode 100644 index 0000000..af63850 --- /dev/null +++ "b/13 \350\224\241\345\230\211\344\271\220/\350\277\220\347\256\227\347\254\224\350\256\260.md" @@ -0,0 +1,349 @@ +## 笔记 + +/* +第5章 SELECT 单表语句 +*/ +#select语句的最简单形式,查看某个常量、变量、表达式的结果 +select 1 as 常量; #别名 外号 +select now() 变量; +select 5+5 "表达式"; +select 6 '测试' + +#查看某个表的数据 +#select * from 表名称; #前提是选择好了,针对哪个库操作,否则会报 No database selected错误 +#select * from 数据库名.表名称; + +use mxdx; +select * from t_employee; + +#查看某些列的数据 +#select 字段列表 from 【数据库名.】表名称; +select ename ,salary from t_employee; +select ename as '名字',salary '工资' from t_employee; -- 使用了别名 + + +#查看某些行的数据 +#select * from 表名称 where 某些行的条件 +#查看薪水少于10000的员工的数据 +select * from t_employee where salary <10000; + +#查看某些行某些列的数据 +#select 某些列 from 表名称 where 某些行的条件 +#查看蔡徐坤的员工编号、薪水 +select ename,salary from t_employee where salary <10000; +select eid,ename,salary from t_employee where ename = '蔡徐坤'; + +#给查询结果列取别名 +#select 字段名 [as] 别名 from 表名; +select eid id,ename as 姓名,salary as '工资' from t_employee where ename = '蔡徐坤'; + +#给表取别名 +#select * from 表名 as 别名 +select ename,salary from t_employee as '员工表';-- 表也可以取别名,但是不能用任何引号 +select ename,salary from t_employee as "员工表"; +select ename,salary from t_employee as 员工表; +-- 给查询结果这个临时表取个临时的名称,用什么用?跨表联合查询的时候用得上 + +#mysql可以在查询结果中使用distinct关键字去重。 +#select distinct 字段列表 from 表名称 [where 条件]; +select distinct salary from t_employee order by salary desc; -- distinct 去除重复,但mysql只能放字段前,而且只有一个字段时生效。 + + + + + +第六章 + +算术运算符 +加:+ +减:- +乘:* +除:/ 正常除,有小数 + div 只保留整数部分 +模:% + +*/ +SELECT 1+2; # 3 +SELECT 2-1; # 1 +SELECT 2*8;# 16 +SELECT 9/2;# 4.5 +SELECT 9 DIV 2; # 4 +SELECT 9.9 DIV 2; # 4 +SELECT 9%2; # 1 + +/* + +大于:> +小于:< +大于等于:>= 大于或于等于都满足 +小于等于:<= 小于或等于 +等于:= <=> 安全等于 +不能用于null判断 id = null 因为null是特殊类型。它与任何类型做运算,结果都是null +不等于:!= 或 <> 不能用于null判断 +*/ +SELECT 1=1;# 1 +SELECT * FROM t_employee WHERE eid <=> 1; +SELECT * FROM t_employee WHERE eid == 1;# java才这么写 +SELECT * FROM t_employee WHERE `commission_pct` = NULL; -- 虽然不报错,但结果就 空的 +SELECT * FROM t_employee WHERE `commission_pct` IS NULL; +SELECT * FROM t_employee WHERE `commission_pct` <=> NULL; + +SELECT * FROM t_employee WHERE `commission_pct` != NULL;-- 虽然不报错,但结果就 空的 +SELECT * FROM t_employee WHERE `commission_pct` IS NOT NULL; +SELECT * FROM t_employee WHERE !(`commission_pct` IS NULL); + + +#查询薪资高于15000的员工信息 +select * from t_employee where salary > 15000; + +#查询不是1号部门的员工 +select * from t_employee where did != 1; + +#查询所有的男员工信息 +select * from t_employee where gender = '男'; + +/* +区间范围:between x and y + x<=值<=y + +​ : 包含两端 between 10 and 20 [10,20] 10<= 值 <=20 +​ not between x and y +集合范围:in (x,y,z) : 只要在这个范围就行 in (1,2,3) =1 or =2 or =3 +​ not in(x,x,x) +​ */ +#查询薪资在[10000,15000]之间的员工 +select * from t_employee where salary >=10000 and salary <= 15000; +select * from t_employee where salary between 10000 and 15000; + +#查询薪资不在[10000,15000]之间的员工 +select * from t_employee where salary <10000 or salary > 15000; +select * from t_employee where salary not between 10000 and 15000; +select * from t_employee where !(salary between 10000 and 15000); + + +#查询1,2,3部门的员工 +select * from t_employee where did in (1,2,3); +select * from t_employee where did =1 or did = 2 or did =3; + + +#查询不是1,2,3部门的员工 +select * from t_employee where !(did in (1,2,3)); +select * from t_employee where did not in (1,2,3); + +/* +模糊查询 like + +%:代表任意个字符 : 0个,一个,两个。。。。 + +_:代表一个字符,如果两个下划线代表两个字符 +*/ +#查询名字中包含“熊”字 的员工 +select * from t_employee where ename like '%熊%'; +#把熊大熊二找出来 +select * from t_employee where ename like '熊_'; + + +#查询由3个字组成的名字,且最后一个是“雨”字 的员工 +select * from t_employee where ename like '_雨'; + + +/* + +逻辑与:&& 或 and 并且 +逻辑或:|| 或 or 或者 +逻辑非:! 或 not 不是 (取反) +逻辑异或: xor 两者只能满足其中一个,不能同时满足,不能同时不满足 +*/ +#查询薪资高于15000的男员工 +select * from t_employee where salary > 15000 and gender = '男'; +select * from t_employee where salary > 15000 && gender = '男'; + + +#查询薪资高于15000,或者他是男员工 +select * from t_employee where salary > 15000 or gender = '男'; +select * from t_employee where salary > 15000 || gender = '男'; + + + + +#查询薪资低于10000,或者入职日期在2016-1-1之前的员工 +select * from t_employee where salary <10000 or hiredate < '2016-1-1'; + + +#查询入职日期不在 2016-1-1之前的员工 +select * from t_employee where hiredate >='2016-1-1'; +select * from t_employee where !(hiredate < '2016-1-1'); + + + +#查询薪资低于10000,但是入职日期是在2016-1-1之后的员工 +#或 +#薪资大于等于10000,但是入职日期是在2016-1-1之前的员工 +select * from t_employee where + (salary < 10000 and hiredate >'2016-1-1') + or + (salary >= 10000 and hiredate <'2016-1-1'); + +select * from t_employee where salary < 10000 xor hiredate < '2016-1-1'; + + +/* +分析:要么满足薪资低于10000,要么满足入职日期在2016-1-1之前 <10000 xor <2016-1-1 +只能满足一个。 +*/ +-- 找出没有奖金的员工 +select * from t_employee where commission_pct is null; + + +/* +1)判断时xx is null +xx is not null +xx <=> null 相当于 xx is null + +*/ + + + +/* + +(2)计算时ifnull(xx,代替值) 当xx是null时,用代替值计算 ifnull(commission_pct,0) + +#计算所有员工的实发工资 +/* +实发工资 = 工资+ 工资*奖金比例 = 工资*(1+比例) +奖金:工资*奖金比例 +实发工资 = salary * (1+奖金比例)= 工资 + 工资*奖金比例 = salary + salary * commission_pct; +*/ + +select eid 编号,ename 姓名,salary 底薪, commission_pct 奖金比例,salary + salary * ifnull(commission_pct,0) as 实发工资 from t_employee; + +-- (2)计算时ifnull(xx,代替值) 当xx是null时,用代替值计算 ifnull(commission_pct,0) + + + + + + + + + + + + + + + +##### 作业 + +第一题 + +```mysql +drop table if exists `employee`; +create database ste charset utf8; +use ste; +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之间的员工信息。 +SELECT * from `employee` WHERE salary >=12000 and salary <=13000; +**要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 +SELECT * from `employee` WHERE +**要求3:**将“李四”的家庭住址改为“广东韶关” / + +**要求4:**查询出名字中带“小”的员工 +SELECT * from `employee` WHERE `name` like '%小%'; +**要求5:**查询出薪资高于11000的男员工信息 +SELECT * from `employee` WHERE salary 。11000 ; +**要求6:**查询没有登记电话号码的员工 / +SELECT * from `employee` WHERE not (tel = null) ; +**要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 +SELECT (addr) from `employee` WHERE salary >12000 ; +**要求8:**查询每个员工的年薪,显示“姓名、年薪” / +SELECT (`name ` salary) from `employee` WHERE salary; +``` + + + + + +第二题 + +````mysql +DROP TABLE IF EXISTS `countries_info`; +create DATABASE ttt charset utf8; +use ttt; +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); +SELECT* from `countries_info` ; ++-------------+-----------+---------+------------+--------------+ +| 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) +select * from `countries_info` WHERE area >3000000 or population> 25000000 ; + +**要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 +select (name, continent,area,population,gdp) from `countries_info` WHERE ; +**要求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 3fd48f20cdc52e9274e16d5b5cbac5c41b587a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E5=98=89=E4=B9=90?= <3196825236@qq.com> Date: Thu, 2 Mar 2023 12:36:48 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=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 --- ...63\350\201\224\346\237\245\350\257\242.md" | 313 ++++++++++++++++++ 1 file changed, 313 insertions(+) create mode 100644 "13 \350\224\241\345\230\211\344\271\220/\345\207\275\346\225\260 \345\205\263\350\201\224\346\237\245\350\257\242.md" diff --git "a/13 \350\224\241\345\230\211\344\271\220/\345\207\275\346\225\260 \345\205\263\350\201\224\346\237\245\350\257\242.md" "b/13 \350\224\241\345\230\211\344\271\220/\345\207\275\346\225\260 \345\205\263\350\201\224\346\237\245\350\257\242.md" new file mode 100644 index 0000000..143e114 --- /dev/null +++ "b/13 \350\224\241\345\230\211\344\271\220/\345\207\275\346\225\260 \345\205\263\350\201\224\346\237\245\350\257\242.md" @@ -0,0 +1,313 @@ +### 笔记 + +、分组函数 + +AVG(x) :求平均值 + +SUM(x):求总和 + +MAX(x):求最大值 + +MIN(x):求最小值 + +COUNT(x):统计记录数 + + + +\#查询全公司员工的实发工资的平均值 + +\#实发工资 = salary * (1 + 奖金比例) + +\#select avg(实发工资) FROM t_employee; + +count(*)是对满足条件的(如果没有where条件,就是对所有记录)记录累加数量 + +count(常量值)等价于count(*) + +count(字段名/表达式) 只统计非NULL值的记录数 + +四、单行函数 + +1、数学函数 +CEIL(2.4) 向上取整 +FLOOR(2.4) 向取整 +ROUND(M,N) 保留M的N位小数,小数位超过N会四舍五入 +TRUNCATE(M,N) 保留M的N位小数,小数位超过N ,直接截断 不四舍五入 +*/ + +#查询全公司的平均薪资,显示小数点后2位 + + +/* +2、字符串函数 +CONCAT(x,y,z) 拼接xyz +CONCAT_WS(a,x,y,z) ,用a来拼接xyz +*/ +SELECT 'hello' + 'world'; #+不是拼接 +SELECT CONCAT('hello','world','java'); +SELECT CONCAT('hello','world','java'),CONCAT_WS('-','hello','world','java'); + +#length求字节个数/长度 +SELECT LENGTH('hello'); #ASCII表范围的字符,一个字符用1个字节 +SELECT LENGTH('闽西大学'); #mysql8默认编码是UTF8,一个汉字时3个字节 +SELECT CHAR_LENGTH('软件工程'); #求字符个数(长度) + + + +#返回某个字符左边/右边的几个字符 +SELECT LEFT('hello', 3),RIGHT('hello',3); + +#trim系列 +SELECT CONCAT('[', TRIM(' hello world '), ']'); -- 清除空格 +SELECT CONCAT('[', LTRIM(' hello world '), ']'); +SELECT CONCAT('[', RTRIM(' hello world '), ']'); +SELECT CONCAT('[', TRIM(BOTH 'x' FROM 'xxxxhelloxxxxxworldxxxx'), ']'); -- 清除两端x +SELECT CONCAT('[', TRIM(LEADING 'x' FROM 'xxxxhelloxxxxxworldxxxx'), ']'); +SELECT CONCAT('[', TRIM(TRAILING 'x' FROM 'xxxxhelloxxxxxworldxxxx'), ']'); + +#截取字符串 +SELECT SUBSTRING_INDEX('www.mxdx.com', '.', 2); +SELECT SUBSTRING_INDEX('www.mxdx.com', '.', -2); +SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('www.mxdx.com', '.', 2),'.',-1); + +/* +3、日期时间函数 +CURDATE() 当前日期 +CURTIME() 当前时间 +NOW() 当前日期+时间 +year() 取年 +month() 取月 +day() 取天 +DATEDIFF(date1,date2)/TIMEDIFF(time1,time2) 返回两个时间的间隔 +*/ +SELECT CURDATE(),CURTIME(),NOW(); +SELECT NOW(),UTC_TIME(); + +#查询这个月过生日的员工 + + +#查询40岁(含)以上的员工 + + +#查询员工姓名和员工的生日中的 月和日 +SELECT ename,CONCAT_WS('-',MONTH(birthday),DAY(birthday)) +FROM t_employee; + + +#查看每个员工的入职天数 +#看今天和员工的入职日期的间隔 + +SELECT ename,hiredate, DATEDIFF(CURDATE(),hiredate) +FROM t_employee; + +#查询入职超过5年的员工 +SELECT ename,hiredate,DATEDIFF(CURDATE(),hiredate)/365 +FROM t_employee +WHERE DATEDIFF(CURDATE(),hiredate)>365*5; + +# +SELECT DATE_FORMAT(CURDATE(), '%y-%c-%e'); + +/* +4、加密函数 +*/ +INSERT INTO t_user VALUES('chai',MD5('123456')); +INSERT INTO t_user VALUES('lin',SHA('123456')); +#同一个表不要使用两种加密函数 + +#查询"chai",密码是“123456” +SELECT * FROM t_user WHERE username='chai' AND PASSWORD=MD5('123456'); +#查询"lin",密码是“123456” +SELECT * FROM t_user WHERE username='lin' AND PASSWORD=MD5('123456'); + +第8章 关联查询 +关联查询:两个或更多个表一起查询。 +又称为联合查询,多表查询。 + +1、关联查询的结果一共有7种: +两个表的记录分为四种: +①A表中的记录能在B表中找到对应的记录 +②A表中的记录在B表中找不到对应的记录 +③B表中的记录可以在A表找到对应的记录 +④B表中的记录在A表中找不到对应的记录 + +(1)A∩B ①③ +(2)A ①② +(3)A-A∩B ② +(4)B ③④ +(5)B-A∩B ④ +(6)A∪B ①②③④ +(7)A∪B-A∩B ②④ + +2、两个表要一起查询,要有前提条件:有关联 +就是有相同逻辑意义和数据类型的字段。 + +3、如何实现7种查询结果 +(1)内连接 inner join +(2)外连接 outer join +左外连接 left outer join 或 left join +右外连接 right outer join 或 right join +全外连接 full outer join 或 full join -- union 代替 + +但是,mysql不支持全外连接,没有full join。 +mysql使用union关键字合并其他的查询结果实现全外连接的效果。 + +内连接 ==> A∩B +左连接 ==> A 或 A-A∩B +右连接 ==> B 或 B-A∩B +全外连接 ==> A∪B 或 A∪B - A∩B + 左连接的A union 右连接的B 得到 A∪B + 左连接的A-A∩B union 右连接B-A∩B 得到 A∪B - A∩B + +4、内连接 inner join +A表 inner join B表 on 关联条件 +*/ + +#查询员工表的员工姓名,和部门编号,部门表的部门名称 +#不显示那些 没有分配部门的员工,也不包括那些没有员工的部门 + +-- 重名问题的产生 +/* +Column 'did' in field list is ambiguous 有重名问题 +原因:did没有指定是哪个表的, +*/ + +-- 笛卡尔积的产生 +/* +上面的sql错误,结果出现了一种现象:笛卡尔积 +原因:无关联条件 +*/ + + + + +/* +5、左连接 left join +(1)A表:A表 left join B表 on A表.关联字段 = B表.关联字段 +(2)A-A∩B: + A表 left join B表 on A表.关联字段 = B表.关联字段 + where 从表.关联字段 is null + 从表是A和B 看占从表位置的表。 +*/ +#查询所有的员工的姓名和部门编号,部门名称,包括那些没有分配部门的员工 + + + +#查询所有的员工的姓名和部门编号,部门名称,只显示那些没有分配部门的员工。 + + + +/* +6、右连接 right join +(1)B表:A表 right join B表 on A表.关联字段 = B表.关联字段 +(2)B-A∩B: + A表 right join B表 on A表.关联字段 = B表.关联字段 + where 从表.关联字段 is null + 从表是A还是B 得看占从表位置的表。 + +思考: + B表 right join A表 on A表.关联字段 = B表.关联字段 结果是A + A表 right join B表 on A表.关联字段 = B表.关联字段 结果是B +*/ +#查询所有部门的编号,部门的名称,以及该部门下所有的员工信息, +#包括那些没有员工的部门 + + + +#查询所有部门的编号,部门的名称,以及该部门下所有的员工信息, +#只显示那些没有员工的部门 + + +/* +主表/父表:部门表是主表 +从表/子表:员工表是从表 + +两个表的关联字段是 did(部门编号) +部门表中是所有部门的信息,是完整的部门信息。 +员工表选择所属部门时,必须参考部门表,选择的部门编号必须在部门表中找得到。 +*/ + + + +/* +7、使用union实现全外连接的效果 +(1)A∪B +转换为 左连接的A union 右连接的B +(2)A∪B - A∩B +转换为 左连接的A-A∩B union 右连接的B-A∩B +*/ +#查询所有员工和所有部门的信息,包括那些没有员工的部门,和没有分配部门的员工 +#当然也包括有员工的部门、和有部门的员工。 + + + +#查询所有员工和所有部门的信息, +#只显示那些没有部门的员工、没有员工的部门。 + + + + +/* +8、特殊的内连接 +*/ +#查询员工和部门的信息,只显示有部门的员工和有员工的部门 + + +/* +9、自连接 +进行关联查询的两个表,本质上是一个表。 + +分析员工表: + 有一个字段是mid,是领导编号,即表示这个员工归谁管。 + 那么这里的领导编号其实就是这个领导他作为员工的员工编号。 + +通过取别名的方式,把一张表虚拟成两张表。 +当然,也要关联字段。 + +*/ +#查询每一个员工的编号,姓名,和他的领导的编号和姓名 + + +/* +10、更多表关联 +n张表关联,关联条件要编写n-1个 +2张表,写1个关联条件 +3张表,写2个关联条件 +.... +*/ + +/* +查询每一个员工的姓名,职位名称,部门名称 +员工的姓名在t_employee表中 +员工的职位名称在t_job表中 +员工的部门名称在t_department表中 + +找关联条件 +(1) t_employee.did = t_department.did +(2) t_employee.job_id = t_job.jid + +A表 inner|left|right join B表 on 关联条件1 + inner|left|right join C表 on 关联条件2 + +*/ + + + +/* +查询员工的姓名,领导的姓名,员工自己职位的名称,领导的职位名称,部门的名称 +分析: + t_employee 当员工表使用 emp + t_employee 当领导表使用 mgr + t_department 部门表 + t_job j1 职位表 + t_job j2 职位表 + +找关联条件 +(1) emp.did = t_department.did +(2) emp.job_id = j2.jid +(3) emp.mid = mgr.eid +(4) mgr.job_id = j1.jid +*/ + +作业 + -- Gitee From 63ab3395e5de805323488e9531e042c13582a1bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E5=98=89=E4=B9=90?= <3196825236@qq.com> Date: Thu, 9 Mar 2023 12:36:17 +0800 Subject: [PATCH 3/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" | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 "13 \350\224\241\345\230\211\344\271\220/\345\255\220\346\237\245\350\257\242 \347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" diff --git "a/13 \350\224\241\345\230\211\344\271\220/\345\255\220\346\237\245\350\257\242 \347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" "b/13 \350\224\241\345\230\211\344\271\220/\345\255\220\346\237\245\350\257\242 \347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..5f173ac --- /dev/null +++ "b/13 \350\224\241\345\230\211\344\271\220/\345\255\220\346\237\245\350\257\242 \347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,116 @@ +| ### 笔记 | | | +| -------- | ------------------------------------------------------------ | ------------------------------------------------------------ | +| | [2](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_2) | | +| | [3](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_3) | ##### 子查询 | +| | [4](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_4) | | +| | [5](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_5) | - 嵌套在其他SQL语句中的查询语句。 | +| | [6](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_6) | - 子查询通常先运行,所以要用()包裹起来。 | +| | [7](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_7) | - 子查询的运行结果,可以作为其他语句的条件,数据源等。 | +| | [8](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_8) | | +| | [9](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_9) | ##### 子查询分三种形式 | +| | [10](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_10) | | +| | [11](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_11) | select 1 from 2 where 3 | +| | [12](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_12) | | +| | [13](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_13) | 1.放在select后面 | +| | [14](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_14) | | +| | [15](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_15) | 2.放在where/having后面 | +| | [16](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_16) | | +| | [17](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_17) | 3.放在from后面,子查询当做一个临时的表使用,必须要给别名。 | +| | [18](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_18) | | +| | [19](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_19) | ##### where或having后的子查询,其子查询的结果有三种形式 | +| | [20](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_20) | | +| | [21](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_21) | 1.单列单个值,可以直接使用比较运算符 | +| | [22](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_22) | | +| | [23](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_23) | 2.单列多个值,可以使用 in ,not in 进行比较 | +| | [24](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_24) | | +| | [25](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_25) | 3.单列多个值,可以使用比较运算符,搭配any,all,等关键字与查询结果进行比较 | +| | [26](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_26) | | +| | [27](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_27) | ##### 子查询结果是单列,可以放在select参与运算,或者是作为字段,也可以放在where / having后当条件。 | +| | [28](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_28) | | +| | [29](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_29) | ##### 子查询结果是多列,多个值,只能放在from后面当临时表使用,并且必须取别名。 | +| | [30](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_30) | | +| | [31](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_31) | ### 作业 | +| | [32](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_32) | | +| | [33](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_33) | ~~~ mysql | +| | [34](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_34) | create database text1; | +| | [35](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_35) | use text1; | +| | [36](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_36) | alter database text1 charset utf8; | +| | [37](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_37) | alter table stuinfo charset utf8; | +| | [38](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_38) | alter table stuExam charset utf8; | +| | [39](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_39) | alter table stuMarks charset utf8; | +| | [40](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_40) | | +| | [41](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_41) | create table stuinfo( | +| | [42](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_42) | stuNO varchar(10) primary key, | +| | [43](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_43) | stuName varchar(10), | +| | [44](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_44) | stuSex varchar(5), | +| | [45](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_45) | stuAge double, | +| | [46](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_46) | stuAddress varchar(20), | +| | [47](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_47) | stuSeat int auto_increment unique | +| | [48](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_48) | ); | +| | [49](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_49) | create table stuExam( | +| | [50](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_50) | examNo int primary key, | +| | [51](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_51) | stuNO varchar(10), | +| | [52](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_52) | writtenExam varchar(10), | +| | [53](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_53) | labExam double, | +| | [54](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_54) | foreign key(stuNo) references stuinfo(stuNo) | +| | [55](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_55) | ); | +| | [56](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_56) | create table stuMarks( | +| | [57](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_57) | examNO int, | +| | [58](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_58) | stuID varchar(10), | +| | [59](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_59) | score double, | +| | [60](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_60) | foreign key(examNO) references stuExam(examNO) | +| | [61](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_61) | ); | +| | [62](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_62) | insert into stuinfo values | +| | [63](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_63) | ('s2501','张秋利','男',20,'美国硅谷',1), | +| | [64](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_64) | ('s2502','李斯文','女',18,'湖北武汉',2), | +| | [65](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_65) | ('s2503','马文才','男',18,'湖南长沙',3), | +| | [66](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_66) | ('s2504','欧阳俊雄','女',21,'湖北武汉',4), | +| | [67](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_67) | ('s2505','梅超风','男',16,'湖北武汉',5), | +| | [68](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_68) | ('s2506','陈旋风','男',19,'美国硅谷',6); | +| | [69](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_69) | insert into stuExam values | +| | [70](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_70) | (1,'s2501','50',70), | +| | [71](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_71) | (2,'s2502','60',65), | +| | [72](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_72) | (3,'s2503','86',70), | +| | [73](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_73) | (4,'s2504','40',80), | +| | [74](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_74) | (5,'s2505','70',85), | +| | [75](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_75) | (6,'s2506','85',90); | +| | [76](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_76) | insert into stuMarks values | +| | [77](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_77) | (1,'s2501',88), | +| | [78](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_78) | (2,'s2501',92), | +| | [79](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_79) | (3,'s2501',53), | +| | [80](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_80) | (4,'s2502',60), | +| | [81](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_81) | (5,'s2502',99), | +| | [82](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_82) | (6,'s2503',82); | +| | [83](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_83) | -- 1.查询出年龄比班上平均年龄大的学生的信息 | +| | [84](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_84) | select * from stuinfo where stuAge>(select round(avg(stuAge),2) from stuinfo); | +| | [85](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_85) | -- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) | +| | [86](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_86) | select t.stuNO,t.stuName,t.stuAge,max(score) from stumarks s left join stuinfo t on s.stuID=t.stuNO group by s.stuID; | +| | [87](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_87) | -- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) | +| | [88](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_88) | select avg(writtenExam+labExam) from stuexam right join stuinfo on stuexam.stuNO=stuinfo.stuNO GROUP BY stuexam.stuNO; | +| | [89](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_89) | -- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) | +| | [90](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_90) | -- 普通查询 | +| | [91](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_91) | select * from stuinfo where stusex='男' and stuage>=20; | +| | [92](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_92) | -- 子查询 | +| | [93](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_93) | select * from stuinfo where stuage=(select stuage from stuinfo where stusex='男' and stuage>=20); | +| | [94](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_94) | -- 5.查询出年龄比所有男生年龄都大的女生的信息 | +| | [95](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_95) | select stuSex,max(stuAge) from stuinfo where stuSex='女' and stuAge> all(select stuAge from stuinfo where stuSex ='男'); | +| | [96](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_96) | -- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) | +| | [97](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_97) | select t.stuNO,t.stuName,t.stuAge,max(score) from stumarks s left join stuinfo t on s.stuID=t.stuNO group by s.stuID; | +| | [98](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_98) | -- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) | +| | [99](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_99) | select * from stuinfo a left join stumarks c on a.stuNO=c.stuID where stuID is NULL; | +| | [100](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_100) | -- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) | +| | [101](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_101) | select * from stuinfo a left join stumarks c on a.stuNO=c.stuID where a.stuNO not in (select stuID from stumarks group by stuID); | +| | [102](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_102) | -- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) | +| | [103](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_103) | select * from stuinfo left join stumarks on stuinfo.stuNO=stumarks.stuID where score>90; | +| | [104](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_104) | -- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) | +| | [105](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_105) | select * from stuinfo left join stumarks on stuinfo.stuNO=stumarks.stuID where score>(select avg(score) from stumarks) | +| | [106](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_106) | -- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) | +| | [107](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_107) | select * from stuinfo left join stumarks on stuinfo.stuNO=stumarks.stuID where stumarks.score > any(select score from stumarks where stumarks.stuID='s2501' ) and stuinfo.stuName != '张秋利' | +| | [108](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_108) | -- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) | +| | [109](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_109) | select * from stuinfo left join stumarks on stuinfo.stuNO=stumarks.stuID where stumarks.score > any(select score from stumarks where stumarks.stuID='s2501' ) and stuinfo.stuName != '张秋利' | +| | [110](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_110) | -- 13.查询班上比所有男生年龄都要大的女生的信息 | +| | [111](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_111) | select stuSex,max(stuAge) from stuinfo where stuSex='女' and stuAge> all(select stuAge from stuinfo where stuSex ='男'); | +| | [112](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_112) | -- 14.查询出只要比某个男生年龄大的女生的信息 | +| | [113](https://gitee.com/class-22-class-02/mysql-base/commit/c3abcf70c458af1f8b8b6e71e08d7de13768110c#96fe666e524fb6acd3b9389512c56be5eff26a71_0_113) | select * from stuinfo where stusex = '女' and stuage > (select min(stuage) from stuinfo where stusex = '男'); | +| | | | +| | | | \ No newline at end of file -- Gitee