diff --git "a/08 \350\224\241\346\263\275\351\222\246/20230222 \350\277\220\347\256\227\347\254\246\347\232\204\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" "b/08 \350\224\241\346\263\275\351\222\246/20230222 \350\277\220\347\256\227\347\254\246\347\232\204\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" index 74d9e66a0d11199ce6aac404815cc4663fb57720..ea391cb525f88e1d2f1023da78e1c2a45ef52208 100644 --- "a/08 \350\224\241\346\263\275\351\222\246/20230222 \350\277\220\347\256\227\347\254\246\347\232\204\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" +++ "b/08 \350\224\241\346\263\275\351\222\246/20230222 \350\277\220\347\256\227\347\254\246\347\232\204\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232.md" @@ -58,10 +58,10 @@ _:表示一个字符,有几个字符就输几个 ```mysql -select * from 表名 where 字段名 like '%宋%'; #查询数据中包含‘宋’字的 -select * from 表名 where 字段名 like '宋%'; #查询数据中‘宋’字开头的 -select * from 表名 where 字段名 like '%宋'; #查询数据中‘宋’字结尾的 -select * from 表名 where 字段名 like '_宋%'; #有‘宋’字单‘宋’前面只有一个字 +select * from 表名 where 字段名 like '%songyang%'; #查询数据中包含‘songyang’字的 +select * from 表名 where 字段名 like 'songyang%'; #查询数据中‘songyang’字开头的 +select * from 表名 where 字段名 like '%songyang'; #查询数据中‘songyang’字结尾的 +select * from 表名 where 字段名 like '_songyang%'; #有‘songyang’字单‘songyang’前面只有一个字 ``` 查询当前数据库的字符集情况:show variabes like '%character%'; @@ -81,6 +81,8 @@ select * from 表名 where 字段名 like '_宋%'; #有‘宋’字单‘宋’ ## 第1题 ```mysql +create database biao; +use biao; drop table if exists `employee`; #创建employee表 CREATE TABLE employee( @@ -112,23 +114,25 @@ INSERT INTO employee(id,`name`,sex,tel,addr,salary)VALUES | 10005 | 王艳 | 女 | NULL | 广东广州 | 14050.16 | ```mysql --- **要求1:**查询出薪资在12000~13000之间的员工信息。 -select * from employee where salary >= 12000 and salary <= 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 salary > 11000 and sex = '男'; --- **要求6:**查询没有登记电话号码的员工 -select * from employee where tel is null; --- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 -select * from employee where salary > 12000 or addr = '广东深圳' or addr = '广东广州'; --- **要求8:**查询每个员工的年薪,显示“姓名、年薪” +# **要求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 name from employee where name like "%小%"; +# **要求5:**查询出薪资高于11000的男员工信息 +select * from employee where sex='男' and salary > 11000; +# **要求6:**查询没有登记电话号码的员工 +select name from employee where tel is null; +# **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 +select name, sex, addr, salary from employee where salary >12000 and sex='男' in ('广东深圳','广东广州'); +# **要求8:**查询每个员工的年薪,显示“姓名、年薪” select name,salary from employee; +# ```mysql +select * from employee; ``` ## 第2题 @@ -136,6 +140,7 @@ select name,salary from employee; countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 ```mysql +use biao; DROP TABLE IF EXISTS `countries_info`; CREATE TABLE `countries_info`( `name` VARCHAR(100), @@ -168,27 +173,28 @@ INSERT INTO countries_info VALUES ``` ```mysql --- **要求1:** 查询大国 的国家名称、人口和面积。 - --- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : - --- - 面积至少为 300万平方公里(即,3000000 km2) - --- - 人口至少为 2500 万(即 25000000) -select name,population,area from `countries_info` where area >= 3000000 or population >= 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值,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; +**要求1:** 查询大国 的国家名称、人口和面积。 +select name,area,population from countries_info; +# 如果一个国家满足下述两个条件之一,则认为该国是 大国 : +# +# - 面积至少为 300万平方公里(即,3000000 km2) +# +# - 人口至少为 2500 万(即 25000000) +select name,area,population from countries_info where area >=2500 and population >=3000000; +# **要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 +select name,continent,area,population,gdp 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 人均贡献 from countries_info ; +# **要求7:**查询人均贡献GDP值低于1000的国家信息。 +select * ,gdp/population from countries_info where gdp/population <1000; +# **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select name 国家名,area 面积,population 人口,area/population 人均国土面积值 from countries_info; +# ```mysql ``` diff --git "a/08 \350\224\241\346\263\275\351\222\246/20230301 \345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" "b/08 \350\224\241\346\263\275\351\222\246/20230301 \345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..0a66be9e4985566a5ff1c4a675d564ed8a745a95 --- /dev/null +++ "b/08 \350\224\241\346\263\275\351\222\246/20230301 \345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" @@ -0,0 +1,290 @@ +# 函数 + +## 1、分组函数 + +- 概念:又称为聚合函数、多行函数,表示会对表中的多行记录一起做一个运算,从而得到结果 + +- 常用类型: + + - AVG(x):求平均值 + - SUM(x):求总和 + - MAX(x) :求最大值 + - MIN(x) :求最小值 + - COUNT(x):统计记录数 + +- 使用方法: + + ```mysql + select 函数(*) from 表名; + select 函数(常量值) from 表名; + -- count(*)或count(常量值):都是统计实际的行数。 + select 函数(字段名) from 表名; + select 函数(表达式) from 表名; + -- count(字段/表达式):只统计“字段/表达式”部分非NULL值的行数。 + ``` + +## 2、数学函数 + +| 函数 | 用法 | +| ------------- | ------------------------------------------------------------ | +| ABS(x) | 返回x的绝对值 | +| CEIL(x) | 返回大于x的最小整数值 | +| FLOOR(x) | 返回小于x的最大整数值 | +| MOD(x,y) | 返回x/y的模 | +| RAND() | 返回0~1的随机值 | +| ROUND(x,y) | 返回参数x的四舍五入的有y位的小数的值 | +| TRUNCATE(x,y) | 返回数字x截断为y位小数的结果 | +| FORMAT(x,y) | 强制保留小数点后y位,整数部分超过三位的时候以逗号分割,并且返回的结果是文本类型的 | +| SQRT(x) | 返回x的平方根 | +| POW(x,y) | 返回x的y次方 | + +- 常用类型 + + ```mysql + round(x,y) #结果有y位小数,小数部分四舍五入 + truncate(x,y) #结果有y位小数,直接截断小数超出部分 + ``` + +## 3、字符串函数 + +## 4、日期时间函数 + +| 函数 | 功能描述 | +| ------------------------------------------------------------ | --------------------------------------------------- | +| CURDATE()或CURRENT_DATE() | 返回当前系统日期 | +| CURTIME()或CURRENT_TIME() | 返回当前系统时间 | +| NOW()/SYSDATE()/CURRENT_TIMESTAMP()/ LOCALTIME()/LOCALTIMESTAMP() | 返回当前系统日期时间 | +| UTC_DATE()/UTC_TIME() | 返回当前UTC日期值/时间值 | +| UNIX_TIMESTAMP(date) | 返回一个UNIX时间戳 | +| YEAR(date)/MONTH(date)/DAY(date)/ HOUR(time)/MINUTE(time)/SECOND(time) | 返回具体的时间值 | +| EXTRACT(type FROM date) | 从日期中提取一部分值 | +| DAYOFMONTH(date)/DAYOFYEAR(date) | 返回一月/年中第几天 | +| WEEK(date)/WEEKOFYEAR(date) | 返回一年中的第几周 | +| DAYOFWEEK() | 返回周几,注意,周日是1,周一是2,…周六是7 | +| WEEKDAY(date) | 返回周几,注意,周一是0,周二是1,…周日是6 | +| DAYNAME(date) | 返回星期,MONDAY,TUESDAY,…SUNDAY | +| MONTHNAME(date) | 返回月份,January,… | +| DATEDIFF(date1,date2)/TIMEDIFF(time1,time2) | 返回date1-date2的日期间隔/返回time1-time2的时间间隔 | +| DATE_ADD(date,INTERVAL expr type)或ADDDATE/DATE_SUB/SUBDATE | 返回与给定日期相差INTERVAL时间段的日期 | +| ADDTIME(time,expr)/SUBTIME(time,expr) | 返回给定时间加上/减去expr的时间值 | +| DATE_FORMAT(datetime,fmt)/ TIME_FORMAT(time,fmt) | 按照字符串fmt格式化日期datetime值/时间time值 | +| STR_TO_DATE(str,fmt) | 按照字符串fmt对str进行解析,解析为一个日期 | +| GET_FORMAT(val_type,format_type) | 返回日期时间字符串的显示格式 | + +- 常用类型 + + ```mysql + curdate() 或者 current_date() #返回当前系统日期 + curtime() 或者 current_time() #返回当前系统时间 + monthname(date) #返回月份 + ``` + +| 参数类型 | 描述 | 参数类型 | 描述 | +| -------- | ---- | ------------- | -------- | +| YEAR | 年 | YEAR_MONTH | 年月 | +| MONTH | 月 | DAY_HOUR | 日时 | +| DAY | 日 | DAY_MINUTE | 日时分 | +| HOUR | 时 | DAY_SECOND | 日时分秒 | +| MINUTE | 分 | HOUR_MINUTE | 时分 | +| SECOND | 秒 | HOUR_SECOND | 时分秒 | +| WEEK | 星期 | MINUTE_SECOND | 分秒 | +| QUARTER | 一刻 | | | + +# 关联查询 + +## 1、关联查询的七种结果 + +```mysql +-- 结果范围为A +select 字段列表 from A表 left join B表 on A表.关联字段 = B表.关联字段; +-- 结果范围为B +select 字段列表 from A表 right join B表 on A表.关联字段 = B表.关联字段; +-- 结果范围为A∪B mysql中用 结果A union 结果B +select 字段列表 from A表 full join B表 on A表.关联字段 = B表.关联字段; +-- 结果范围为A∩B +select 字段列表 from A表 inner join B表 on A表.关联字段 = B表.关联字段; +-- 结果范围为A-A∩B 假设A表为子表 +select 字段列表 from A表 left join B表 on A表.关联字段 = B表.关联字段 where A表.关联字段 is null; +-- 结果范围为B-A∩B 假设A表为子表 +select 字段列表 from A表 right join B表 on A表.关联字段 = B表.关联字段 where A表.关联字段 is null; +-- 结果范围为A∪B-A∩B mysql中用 结果A-A∩B union 结果B-A∩B +select 字段列表 from A表 full join B表 on A表.关联字段 = B表.关联字段 where A表.关联字段 is null or B表.关联字段 is null; +``` + +## 2、关联查询的几种情况 + +### 1、内连接:inner join ... on + +- 结果:A表 ∩ B表 + + +### 2、左连接:A left join B on + +- 结果:A表全部、A表- A∩B + + +### 3、右连接:A right join B on + +- 结果:B表全部、B表-A∩B + + +### 4、全外连接:full outer join ... on + +- 注:mysql不支持这个关键字,mysql使用union(合并)结果的方式代替 + +- 结果:A表∪B表、 A表结果 union 、B表的结果、A∪B - A∩B、A表- A∩B结果 union 、B表-A∩B结果 + +## 七大子句顺序 + +1. from:从那些表中筛选 +2. on:关联多表查询时,去除笛卡尔积 +3. where:从表中筛选的条件 +4. group by:分组依据 +5. having:在统计结果中再次筛选(with rollup) +6. order by:排序 +7. limit:分页 + +注:必须按照从上到下的顺序编写 + + +# 作业 + +### 新建 + +```MySQL +create database cai charset utf8; +use cai; +create table student( + sno varchar(20) not null primary key, + sname varchar(20) not null, + ssex varchar(20) not null , + sbirthday datetime, + class varchar(20) +); +create table teacher( + tno varchar(20) not null primary key, + tname varchar(20) not null, + tsex varchar(20) not null, + tbirthday datetime, + prof varchar(20), + depart varchar(20) +); +create table course( + cno varchar(20) not null primary key , + cname varchar(20) not null, + ton varchar(20) not null, + foreign key (ton) references teacher(tno) +); + +create table score( + sno varchar(20) not null , + cno varchar(20) not null , + degree decimal(4,1), + foreign key(sno)references student(sno), + foreign key(cno)references course(cno) +); + + +``` + + + +### 插入数据 + +~~~mysql +insert into student value + (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 value + (804,'李诚','男','1958-12-2','副教授','计算机系'), + (856,'张旭','男','1969-3-12','讲师','电子工程系'), + (825,'王萍','女','1972-5-5','助教','计算机系'), + (831,'刘冰','女','1977-8-14','助教','电子工程系') +; +insert into course value + ('3-105','计算机导论',825), + ('3-245','操作系统',804), + ('6-166','数字电路',856), + ('9-888','高等数学',831) +; +insert into score value + (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); + +~~~ + + + +### 查询 + +```mysql +# ① 查询Score表中的最高分的学生学号和课程号。 +select cno,sno from score where degree in(select max(degree) from score); +# ② 查询所有学生的Sname、Cno和Degree列。 +select Sname,Cno,a.Degree from student join Score a on Student.Sno = a.Sno; +# ③ 查询所有学生的Sno、Cname和Degree列。 +select sno,cname,degree from score inner join course c on score.cno = c.cno; +# ④ 查询所有学生的Sname、Cname和Degree列。 +⑥ select a.sname,c.Cname,S.Degree from student a left join Score S on a.Sno = S.Sno join Course C on C.Cno = S.Cno; +# ⑤ 查询“95033”班学生的平均分。 +select avg(Degree) from score left join Student S on Score.Sno = S.Sno where class=95033; +# ⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select * from student a join score s on a.sno = s.sno where s.cno='3-105' and degree > any(select s.degree from score s where s.sno=109); +# ⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +select * from score a where sno in (select sno from score group by sno having count(*)>1)and degree<(select max(degree) from score b where a.cno=b.cno ); + +# ⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select * from student a join score s on a.sno = s.sno 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); +# ⑩ 查询“张旭“教师任课的学生成绩。 +select s.degree from score s join course c on c.cno = s.cno where c.ton=(select t.tno from teacher t where tname='张旭'); +# 11 查询选修某课程的同学人数多于5人的教师姓名。 +select tname from teacher where tno in (select ton from course where cno in (select cno from score sc group by cno having count(sc.sno)>5)); + +# 12 查询出“计算机系“教师所教课程的成绩表。 +select tname from teacher where tno in (select ton from course where cno in (select cno from score sc group by cno having count(sc.sno)>5)); +# 13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select tname,prof from teacher where depart='电子工程系' and prof not in(select prof from teacher where depart='计算机系') union select tname,prof from teacher where depart='计算机系' and prof not in(select prof from teacher where depart='电子工程系'); + +# 14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select sno,cno,degree from score where degree > (select max(degree) from score group by cno having cno='3-245') order by degree desc ; +# 15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +select sno,cno,degree from score where degree > (select max(degree) from score group by cno having cno='3-245') +# 16 查询成绩比该课程平均成绩低的同学的成绩表。 +select * from score where sno in (select sno from score where cno='3-245'and degree < (select avg(degree) from score where cno='3-245') union select sno from score where cno='3-105'and degree < (select avg(degree) from score where cno='3-105') union select score.sno from score where cno='6-166'and degree < (select avg(degree) from score where cno='6-166')); + +# 17 查询所有任课教师的Tname和Depart. +select tname,depart from teacher where tno in (select ton from course where cno in (select cno from score )); + +# 18 查询所有未讲课的教师的Tname和Depart. +select tname,depart from teacher where tno not in (select ton from course where cno in (select cno from score )); + +# 19 查询“男”教师及其所上的课程。 +select cname from course where ton in (select ton from teacher where tsex='男') +# 20 查询最高分同学的Sno、Cno和Degree列。 +select sno,cno,degree from score where degree in (select max(degree)from score); +# 21 查询和“李军”同性别的所有同学的Sname. +select sname,ssex from student where ssex in (select ssex from student where sname='李军'); +# 22 查询和“李军”同性别并同班的同学Sname. +select sname,ssex from student where ssex in (select ssex from student where sname='李军'); +# 23 查询所有选修“计算机导论”课程的“男”同学的成绩表 +select s.sno,s.cno,s.degree from score s left join course c on c.cno = s.cno join student s2 on s2.sno = s.sno where c.cname='计算机导论'and s2.ssex='男'; + +``` diff --git "a/08 \350\224\241\346\263\275\351\222\246/20230307 \345\255\220\346\237\245\350\257\242\345\222\214\344\275\234\344\270\232\346\234\252\345\256\214\346\210\220.md" "b/08 \350\224\241\346\263\275\351\222\246/20230307 \345\255\220\346\237\245\350\257\242\345\222\214\344\275\234\344\270\232\346\234\252\345\256\214\346\210\220.md" new file mode 100644 index 0000000000000000000000000000000000000000..120916c34e15eec82abfbb795e8b6e8b8e6c6257 --- /dev/null +++ "b/08 \350\224\241\346\263\275\351\222\246/20230307 \345\255\220\346\237\245\350\257\242\345\222\214\344\275\234\344\270\232\346\234\252\345\256\214\346\210\220.md" @@ -0,0 +1,99 @@ +## 作业 + +### 新建 + +```mysql +create database cai charset utf8; +use cai; +create table stuinfo( + stuNO varchar(20)primary key , + stuName varchar(20), + stuSex enum('男','女'), + stuAge int, + stuAddress varchar(20), + stuSeat int +); +create table stuExam( + examNo int primary key , + stuNO varchar(20), + writtenExam int, + labExam int, + foreign key (stuNO) references stuinfo(stuNO) +); +create table stuMarks( + examNo int, + stuID varchar(20), + score int, + foreign key (examNo) references stuExam(examNo), + foreign key (stuID) references stuinfo(stuNO) +); +``` + +### 插入 + +~~~mysql +insert into stuinfo value + ('s2501','张秋利','男',20,'美国硅谷',1), + ('s2502','李斯文','女',18,'湖北武汉',2), + ('s2503','马文才','男',18,'湖南长沙',3), + ('s2504','欧阳俊雄','女',21,'湖北武汉',4), + ('s2505','梅超风','男',20,'湖北武汉',5), + ('s2506','陈旋风','男',19,'美国硅谷',6 + ); +insert into stuExam value + (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 +); +~~~ + +### 作业内容 + +~~~mysql +# 1.查询出年龄比班上平均年龄大的学生的信息 +select AVG(stuAge) from stuinfo; +select *, AVG(stuAge) 平均年龄 from stuinfo where stuAge>(select AVG(stuAge) from stuinfo); +# 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select si.stuNO,si.stuName,stuSex,max(sM.score) from stuinfo si left join stuMarks sM on si.stuNO = sM.stuID; +# 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select si.stuNO,si.stuName,si.stuAge,avg(sE.labExam) 考试平均分 from stuinfo si left join stuExam sE on si.stuNO = sE.stuNO; +# 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuAge>=20 and stuSex='男'; + +select stuAge from stuinfo where stuAge>=20; +select * from stuinfo where stuSex='男' and stuAge in (select stuAge from stuinfo where stuAge>=20); +# 5.查询出年龄比所有男生年龄都大的女生的信息 +select max(stuAge) from stuinfo where stuSex='男'; +select * from stuinfo where stuSex='女' and stuAge>(select max(stuAge) from stuinfo where stuSex='男'); +# 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select distinct stuinfo.* from stuinfo join stumarks on stuNO = stuMarks.stuID where stuID not in (select stuID from stuMarks where score<60); +# 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select distinct si.* from stuinfo si left join stuMarks sM on si.stuNO = sM.stuID where si.stuNO IN (select stuID from stuMarks); +# 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select distinct si.* from stuinfo si left join stuMarks sM on si.stuNO = sM.stuID where si.stuNO not IN (select stuID from stuMarks); +# 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select * from stuinfo left join stuMarks sM on stuinfo.stuNO = sM.stuID where sM.score>90; +# 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) + +# 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) + +# 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select max(score) from stuMarks left join stuinfo s on s.stuNO = stuMarks.stuID where s.stuName='张秋利'; +select * from stuMarks right join stuinfo s on stuMarks.stuID = s.stuNO where score>(select max(score) from stuMarks join stuinfo s on s.stuNO = stuMarks.stuID where s.stuName='张秋利') ; +# 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stuinfo where stuAge > (select max(stuAge) from stuinfo where stuSex='男') and stuSex='女'; +# 14.查询出只是比某个男生年龄大的女生的信息 +select * from stuinfo where stuAge > (select max(stuAge) from stuinfo where stuName='李斯文') and stuSex='女'; +~~~ +