SQL select总结(基于选课系统)

news/2024/5/19 0:27:57/文章来源:https://blog.csdn.net/lanlanlannan/article/details/130116142

表详情:

学生表:
请添加图片描述

学院表:
请添加图片描述

学生选课记录表:

请添加图片描述

课程表:

请添加图片描述

教师表:

请添加图片描述

查询:

1. 查全表

-- 01. 查询所有学生的所有信息
-- 方法一:会更复杂,进行了两次查询,第一次查询这个表的列,第二次根据查到的列再查每列的数据
select * from teachers;
-- 方法二:直接查询该实体所有属性
select stu_id, stu_name, stu_sex, stu_birth, stu_addr, col_id from students;

2. 投影(select)和别名(alias / as)

-- 02. 查询学生的学号、姓名和籍贯(投影和别名)	-	别名:alias
select stu_id as 学号, stu_name as 姓名, stu_addr as 籍贯from students;-- 03. 查询所有课程的名称及学分(投影和别名)
select cou_name as 名称, cou_credit as 学分from courses;

3. 数据筛选(where 加 and / or 加 分支 加 比较符)

-- 04. 查询所有女学生的姓名和出生日期(数据筛选)
select stu_name, stu_birthfrom studentswhere stu_sex = 0;-- 05. 查询籍贯为“四川成都”的女学生的姓名和出生日期(数据筛选)
select stu_name, stu_birthfrom studentswhere stu_sex = 0 and stu_addr = '四川成都';-- 06. 查询籍贯为“四川成都”或者性别是女的学生(数据筛选)
select stu_name, stu_birthfrom studentswhere stu_sex = 0 or stu_addr = '四川成都';-- 07. 查询所有80后学生的姓名、性别和出生日期(数据筛选)
select stu_name, stu_sex, stu_birth
from students
where stu_birth >= '1980-1-1' and stu_birth <= '1989-12-31';select stu_name, stu_sex, stu_birth
from students
where stu_birth between '1980-1-1' and '1989-12-31';-- 将01改为男女-- MySQL方言select stu_name as 姓名, if(stu_sex,'男','女') as 性别, stu_birth as 出生日期from studentswhere stu_birth between '1980-1-1' and '1989-12-31';-- 标准SQLselect stu_name as 姓名, case stu_sex when 1 then '男' when  0 then '女' else '未知' end as 性别from studentswhere stu_birth between '1980-1-1' and '1989-12-31';-- 08. 查询学分大于2分的课程名称和学分(数据筛选)
select cou_name, cou_creditfrom courseswhere cou_credit > 2;-- 09. 查询学分是奇数的课程的名称和学分(数据筛选)
select cou_name, cou_creditfrom courseswhere cou_credit%2 <> 0;-- 10. 查询选择选了1111的课程考试成绩在90分以上的学生学号(数据筛选)
select stu_idfrom recordswhere cou_id = 1111 and score > 90;-- 11. 查询名字叫“杨过”的学生的姓名和性别
select stu_name, stu_sexfrom studentswhere stu_name = '杨过';

4.模糊查询(通配符和正则)

-- 12. 查询姓“杨”的学生姓名和性别(模糊查询)
-- wild card - 通配符 - % - 零个或任意多个字符
select stu_name, stu_sexfrom studentswhere stu_name like '杨%';-- 13. 查询姓“杨”名字两个字的学生姓名和性别(模糊查询)
-- wild card - 通配符 - _ - 精确匹配一个字符
select stu_name, stu_sexfrom studentswhere stu_name like '杨_';-- 14. 查询姓“杨”名字三个字的学生姓名和性别(模糊查询)
select stu_name, stu_sexfrom studentswhere stu_name like '杨__';-- 15. 查询名字中有“不”字或“嫣”字的学生的姓名(模糊查询)
select stu_namefrom studentswhere stu_name like '%不%' or stu_name like '%嫣%';select stu_namefrom studentswhere stu_name regexp '[\\u4e00-\\u9fa5]*?[不嫣][\\u4e00-\\u9fa5]*?';select stu_namefrom studentswhere stu_name like '%不%'union
select stu_namefrom studentswhere stu_name like '%嫣%';-- 16. 查询姓“杨”或姓“林”名字三个字的学生的姓名(正则表达式模糊查询)
select stu_namefrom studentswhere stu_name regexp '[杨林][\\u4e00-\\u9fa5]{2}';

5. 空值和去重(三值逻辑和trim)

-- 17. 查询没有录入籍贯的学生姓名(空值处理)
-- 三值逻辑 - true / false / unknownselect stu_namefrom studentswhere stu_addr is null;-- 如果遇到空字符串
-- trim - 修剪字符串左右两端指定的字符(默认修建空格)
update students set stu_addr = ' ' where stu_id = 3011;select stu_namefrom studentswhere stu_addr is null or trim(stu_addr) = '';-- 18. 查询录入了籍贯的学生姓名(空值处理)
select stu_namefrom studentswhere stu_addr is not null and trim(both ' ' from stu_addr) <> '';-- 19. 查询学生选课的所有日期(去重)
select distinct sel_datefrom records ;-- 20. 查询学生的籍贯(空值处理和去重)
select distinct stu_addrfrom studentswhere stu_addr is not null and trim(stu_addr) <> '';

6. 排序

-- 21. 查询男学生的姓名和生日按年龄从大到小排列(排序)
-- 排序 asc (默认)升序 desc 降序
select stu_name, stu_birthfrom studentswhere stu_sex = 1
order by stu_birth asc, stu_id desc;

7. 日期函数和数值函数

-- 查询现在的日期时间-- select current_timestamp();	2023-04-12 09:15:58-- select now();	2023-04-12 09:14:16-- select curdate();    2023-04-12-- select curtime();	09:15:24
-- 日期差-- datediff(大日期,小日期) 两个日期相差的天数-- timestampdiff(单位,小日期,大日期)
-- 22. 将上面查询中的生日换算成年龄(日期函数、数值函数)
select stu_name as 姓名, datediff(curdate(),stu_birth) div 365 as 年龄from studentswhere stu_sex = 1
order by stu_birth asc;select stu_name as 姓名, timestampdiff(year,stu_birth,curdate()) as 年龄from studentswhere stu_sex = 1
order by stu_birth desc;

8. 聚合函数(最大/方差/平均…coalesce)

-- 有多条数据 最后只给出一条数据
-- 规约 reduce / aggregate 聚合
-- python中的规约: filter / map / reduce (max/min/sum/avg/count)

总体方差:
σ2=1N∑i=1N(xi−μ)2\sigma^{2} = \frac{1}{N} \sum_{i=1}^{N} (x_{i} - \mu)^{2} σ2=N1i=1N(xiμ)2
总体标准差:
σ=1N∑i=1N(xi−μ)2\sigma = \sqrt{\frac{1}{N} \sum_{i=1}^{N} (x_{i} - \mu)^{2}} σ=N1i=1N(xiμ)2
样本方差:
s2=1n−1∑i=1n(xi−xˉ)2s^{2} = \frac{1}{n - 1} \sum_{i=1}^{n} (x_{i} - \bar{x})^{2} s2=n11i=1n(xixˉ)2
样本标准差:
s=1n−1∑i=1n(xi−xˉ)2s = \sqrt{\frac{1}{n - 1} \sum_{i=1}^{n} (x_{i} - \bar{x})^{2}} s=n11i=1n(xixˉ)2

markdown写数学公式:

https://www.zybuluo.com/codeep/note/163962

-- SQL聚合函数:
-- max / min / avg / sum / count
-- stddev_pop(总体标准差)
-- stddev_samp(样本标准差)
-- var_pop(总体方差)
-- var_samp(样本方差)
-- 23. 查询年龄最大的学生的出生日期(聚合函数)
select min(stu_birth)from students;-- 24. 查询年龄最小的学生的出生日期(聚合函数)
select max(stu_birth)from students;-- 25. 查询编号为1111的课程考试成绩的最高分(聚合函数)
select max(score)from recordswhere cou_id = 1111;-- 26. 查询学号为1001的学生考试成绩的最低分(聚合函数)
select min(score)from recordswhere cou_id = 1111;-- 27. 查询学号为1001的学生考试成绩的平均分和标准差(聚合函数)
select avg(score), stddev_pop(score), var_pop(score)from recordswhere stu_id = 1001;-- 28. 查询学号为1001的学生考试成绩的平均分,如果有null值,null值算0分(聚合函数)
-- ifnull 是MySQL的方言
select avg(ifnull(score,0))from recordswhere stu_id = 1001;-- 标准SQL的函数 coalesce(值1,值2,值3,...) 返回第一个非空的值
select avg(coalesce(score,0))from recordswhere stu_id = 1001;-- count(*) 返回所有的行数select sum(score) / count(*)from recordswhere stu_id = 1001;

9. 分组和聚合函数(having/rollup/取整)

-- 29. 查询男女学生的人数(分组和聚合函数)
select case stu_sex when 1 then '男' else '女' end as 性别, count(*) as 人数from studentsgroup by stu_sex;-- 30. 查询每个学院学生人数(分组和聚合函数)
-- with rollup 总计 rollup维度上卷
select col_id as 学院, count(*) as 学生人数from studentsgroup by col_idwith rollup;-- 31. 查询每个学院男女学生人数(分组和聚合函数)
select col_id as 学院, stu_sex as 性别, count(*) as 学生人数from studentsgroup by col_id, stu_sexwith rollup;-- 32. 查询选课学生的学号和平均成绩(分组和聚合函数)
-- round() 保留几位小数, ceil向大取整, floor向小取整
select stu_id, round(avg(score),1), ceil(avg(score)), floor(avg(score))from recordsgroup by stu_id;-- 33. 查询平均成绩大于等于90分的学生的学号和平均成绩(分组和聚合函数)
-- 分组之前的筛选数据用where子句;分组之后的筛选数据用having子句
select stu_id, round(avg(score),1)from recordsgroup by stu_id
having avg(score) >= 90;-- 34. 查询所有课程成绩大于80分的同学的学号(分组和聚合函数)
select stu_idfrom recordsgroup by stu_id
having min(score) > 80;

10. 嵌套查询(定义变量/any/all)

-- 35. 查询年龄最大的学生的姓名(嵌套查询)
-- 嵌套查询:把一个查询结果作为另外一个查询的一部分来使用,也称为子查询。
select min(stu_birth)from students;
select stu_namefrom studentswhere stu_birth = '1985-04-17';-- 定义变量:-- 赋值方法1:用户自定义变量前面要有@,给变量赋值前面要加setset @min_birth = (select min(stu_birth) from students);-- 赋值方法2:海象运算符select @min_birth := (select min(stu_birth) from students);select stu_namefrom studentswhere stu_birth = @min_birth;-- 一步到位
select stu_namefrom studentswhere stu_birth = (select min(stu_birth) from students);-- 36. 查询选了两门以上的课程的学生姓名(嵌套查询/分组/数据筛选)
-- 符合要求的学号
select stu_idfrom recordsgroup by stu_id
having count(score) > 2;-- 通过学号查名字
select stu_namefrom studentswhere stu_id in (select stu_idfrom recordsgroup by stu_idhaving count(*) > 2);
-- any 用any后的任意一个值和前面做对比
-- all 前面和后面的所有相等
select stu_namefrom studentswhere stu_id = any (select stu_idfrom recordsgroup by stu_idhaving count(*) > 2);

11. 连接查询(交叉连接/内连接/自然连接删除线格式 )

请添加图片描述

-- 37. 查询学生的姓名、生日和所在学院名称(连接查询)
-- from后面写多个表,是from后面表的笛卡尔积-- 方法1:笛卡尔积
select stu_name, stu_birth, col_name, students.col_id, colleges.col_idfrom students,collegeswhere students.col_id = colleges.col_id;-- 方法2:cross join交叉连接
select stu_name, stu_birth, col_name, students.col_id, colleges.col_idfrom students cross join collegeswhere students.col_id = colleges.col_id;-- 方法3:内连接
select stu_name, stu_birth, col_namefrom students inner join collegeswhere students.col_id = colleges.col_id;-- 方法4:自然连接
-- 自然连接会根据两张表的同名列进行匹配
-- 自然连接没有同名列,同名列都合成一个列了
select stu_name, stu_birth, col_namefrom students natural join colleges;-- 38. 查询学生姓名、课程名称以及成绩(连接查询)
-- 笛卡尔积
select stu_name, cou_name , scorefrom students, records, courseswhere students.stu_id = records.stu_id and records.cou_id = courses.cou_id;-- 自然连接
select stu_name, cou_name , scorefrom students natural join records natural join courseswhere score is not null;

12. 分页查询

-- 39. 上面的查询结果按课程和成绩排序取前5条数据(分页查询)
-- limit和offset是方言
select stu_name, cou_name , scorefrom students natural join records natural join courseswhere score is not nullorder by cou_id asc, score desclimit 5;-- 40. 上面的查询结果按课程和成绩排序取第6-10条数据(分页查询)
select stu_name, cou_name , scorefrom students natural join records natural join courseswhere score is not nullorder by cou_id asc, score desclimit 5
offset 5;-- 41. 上面的查询结果按课程和成绩排序取第11-15条数据(分页查询)
select stu_name, cou_name , scorefrom students natural join records natural join courseswhere score is not nullorder by cou_id asc, score desclimit 5
offset 10;

13. 嵌套和连接查询

-- 42. 查询选课学生的姓名和平均成绩(嵌套查询和连接查询)
select stu_name, avg_scorefrom studentsnatural join (select stu_id, round(avg(score),1) as avg_scorefrom recordsgroup by stu_id) as temp;-- 43. 查询学生的姓名和选课的数量(嵌套查询和连接查询)
select stu_name, total1, total2from studentsnatural join (select stu_id, count(*) as total1, count(score) as total2from recordsgroup by stu_id) as temp;

14. 外连接

-- 外连接-- 左外连接(left outer join / left join):左表(写到join左边的表)取到所有的数据,不满足连表条件的地方填空值。-- 右外连接(right outer join / right join):右表(写到join右边的表)取到所有的数据,不满足连表条件的地方填空值。-- 全外连接(full outer join / full join):左右两张表都要取到所有的数据,不满足连表条件的地方填空值。(MySQL不支持)-- 可以通过左外连接union右外连接
-- 44. 查询每个学生的姓名和选课数量(左外连接和嵌套查询)
-- 选课表里没有她们的数据 自然连接和内连接不好用
select stu_name, total1, total2from studentsleft join (select stu_id, count(*) as total1, count(score) as total2from recordsgroup by stu_id) as tempon students.stu_id = temp.stu_id;-- 45. 查询没有选课的学生的姓名(左外连接和数据筛选)
-- 查是哪几个没有选课(用空值填的)
select stu_name, rec_idfrom students left join recordson students.stu_id = records.stu_id;
-- 通过空值查没选课的名字
select stu_namefrom students left join recordson students.stu_id = records.stu_idwhere rec_id is null;

SQL执行顺序

请添加图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_285703.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于灵动微SPIN系列开发的水泵方案介绍 以 MM32SPIN040C/MM32SPIN560C为主控

水泵是输送液体或使液体增压的机械。它将原动机的机械能或其他外部能量传送给液体&#xff0c;使液体能量增加&#xff0c;主要用来输送液体包括水、油、酸碱液、乳化液、悬乳液和液态金属等。 水泵以 MM32SPIN040C/MM32SPIN560C为主控。 水泵方案 MCU: MM32SPIN系列 1.输入…

【JavaWeb】后端(Maven+SpringBoot+HTTP+Tomcat)

目录一、Maven1.什么是Maven?2.Maven的作用?3.介绍4.安装5.IDEA集成Maven6.IDEA创建Maven项目7.IDEA导入Maven项目8.依赖配置9.依赖传递10.依赖范围11.生命周期二、SpringBoot1.Spring2.SpringBoot3.SpringBootWeb快速入门二、HTTP1.HTTP-概述2.HTTP-请求协议3.HTTP-响应协议…

机器学习实战:Python基于Logistic逻辑回归进行分类预测

目录1 前言1.1 Logistic回归的介绍1.2 Logistic回归的应用2 iris数据集数据处理2.1 导入函数2.2 导入数据2.3 简单数据查看3 可视化3.1 条形图/散点图3.2 箱线图3.3 三维散点图4 建模预测4.1 二分类预测4.2 多分类预测5 讨论1 前言 1.1 Logistic回归的介绍 逻辑回归&#xff…

产品知识沉淀

梁宁-产品思维30讲 看一个人或看一个产品&#xff0c;可以由表及里的五层来做观察和判断&#xff1a;感知层、角色层、资源层、能力圈和存在感 存在感之于人就好像生存之于动物一样&#xff0c;是触发情绪和推动行动的开关。 动物的状态和情绪&#xff0c;都是关乎它的生存需…

Stearic acid-mPEG,mPEG-STA,甲氧基PEG-单硬脂酸,具有优异疏水性

●外观以及性质&#xff1a; 硬脂酸是一种具有优异疏水性的18碳饱和脂肪酸脂质。PEG修饰的硬脂酸是一种具有亲水性和疏水性的优良的两亲性聚合物。聚乙二醇化脂质是一种优良的脂质体形成材料&#xff0c;可用于药物递送、基因转染和疫苗递送。硬脂酸是十八烷酸CH3&#xff08;C…

微信小程序开发 | API应用案例(下)

API应用案例&#xff08;下&#xff09;6.1【案例5】模拟时钟6.1.1 案例分析6.1.2 前导知识6.1.3 钟表页面布局6.1.4 钟表页面绘制6.2【案例6】罗盘动画6.2.1 案例分析6.2.2 前导知识6.2.3 设计罗盘页面布局6.2.4 手指触摸旋转罗盘6.2.5 单击按钮操作罗盘6.3【案例7】文件上传与…

关于药物|新药|药品市场调研报告(实操资料分享)

药品市场调研报告是指对药品行业进行详细的市场情况研究和分析。往往伴随着药品市场调研目的地不同&#xff0c;如战略探索、新药开发、投资决策等&#xff0c;报告编辑的内容要点要求也不一样。但总的核心要点内容笔者已提炼&#xff0c;如下&#xff1a; 一、药品市场调研报告…

Python学习笔记--判断语句

&#xff08;一&#xff09; 布尔类型和比较运算符 1. 布尔类型&#xff1a;判断结果 True&#xff1a;表示真&#xff08;是、肯定&#xff09; False&#xff1a;表示假&#xff08;否、否定&#xff09; """ 演示布尔类型的定义 以及比较运算符的应用 "…

【花雕学AI】找出合适的提示词—让ChatGPT发挥出最大的潜力与价值

ChatGPT 是一种基于人工智能技术的自然语言处理系统&#xff0c;它可以回答各种问题&#xff0c;提供有用的信息和建议。然而&#xff0c;要让 ChatGPT 发挥出最大的潜力和价值&#xff0c;我们需要使用一些提示词来帮助它更好地理解我们的问题和需求。这些提示词包括明确、详细…

文件上传漏洞 --- php邂逅windows通用上传缺陷

目录 后端源码 前端源码 后端代码审计 方式一绕过原理 --- 冒号加特性 验证及结果 方式二绕过原理 --- 数据流 验证及结果 环境需求 php5.2.17IIS环境&#xff0c;可以下载phpstuday2018来满足环境的要求。 后端源码 <?php //U-Mail demo ... if(isset($_POST[sub…

项目3:积分等级表接口的开发和使用(后台)

项目3&#xff1a;积分等级表接口的开发和使用 1.service-core的controller创建admin包 2.对积分登记表完成增删改查 3.配置swagger接口生成器和ui 4.统一设置返回结果 5.统一设置异常处理 6.统一日志处理 项目3&#xff1a;积分等级表接口的开发和使用 1.service-core的…

编码与加密基础笔记

文章目录&#x1f449;1、ASCII 编码&#x1f449;2、了解Base64&#x1f449;3、MD5消息摘要算法&#x1f449;4、对称加密与 AES&#x1f449;5、非对称加密与 RSA参考书籍《Python 3 反爬虫原理与绕过实战》&#x1f449;1、ASCII 编码 ASCII编码实际上约定了字符串和二进制…

unity的基本窗口界面简要介绍

呜呜呜呜呜呜呜呜呜&#xff0c;怎么可能不难过啊&#xff0c;这tm比失恋难受 学习学习&#xff0c;我要移情别恋 打开一个项目&#xff0c;在左上角或者其他地方&#xff0c;能看到以下界面 Scene&#xff1a;场景编辑窗口 在这个界面我们可以自由切换视角观看场景&#xff0…

Web前端基础——盒子模型

&#xff08;1&#xff09;盒子模型的作用&#xff1a; 布局网页&#xff0c;摆放盒子和内容 &#xff08;2&#xff09;盒子模型重要组成部分&#xff1a; 内容区域 - width & height内边框 - padding&#xff08;出现在内容与盒子边缘之间&#xff09;边框线 - border外…

MySQL---数据类型

文章目录前言一、数据类型分类二、数值类型1.tinyint类型2.bit类型三、小数类型1.float2. decimal三、字符串类型1.char2.varchar3.char和varchar比较四、日期和时间类型五、enum和set我们如何找到性别是男或者女呢?我们如何找到爱好有rapper呢?总结前言 正文开始!!! 一、数…

NE555 Motor LED Chaser

文章目录1.前言2.资料下载1.前言 这个是从YouTube上搬运来的&#xff0c;如图所示 2.资料下载 所需材料 #1# 10k resistor 1 #2# 10k variable resistor 1 #3# 10uf capacitor 1 #4# 3mm blue led 4 #5# 3mm yellow led 4 #6# 3mm red led 4 #7# 3mm green led 4 #8# 3mm w…

新规拉开中国生成式AI“百团大战”序幕?

AI将走向何方&#xff1f; ChatGPT在全球范围掀起的AI热潮正在引发越来越多的讨论&#xff0c;AI该如何管理&#xff1f;AI该如何发展&#xff1f;一系列问题都成为人们热议的焦点。此前&#xff0c;马斯克等海外名人就在网络上呼吁OpenAI暂停ChatGPT的模型训练和迭代&#xf…

OpenCV实战之人脸美颜美型(六)——磨皮

1.需求分析 有个词叫做“肤若凝脂”,直译为皮肤像凝固的油脂,形容皮肤洁白且光润,这是对美女的一种通用评价。实际生活中我们的皮肤多少会有一些毛孔、斑点等表现,在观感上与上述的“光润感”相反,因此磨皮也成为美颜算法中的一项基础且重要的功能。让皮肤变得更加光润,就…

记录一次使用宝塔部署Vue3 + Spring boot项目遇到的问题

一. Vue相关配置 1. 修改Vue配置文件&#xff1a;&#xff08;vue.config.js&#xff09;,没有该文件则在项目根目录下新建 const { defineConfig } require(vue/cli-service)module.exports defineConfig({transpileDependencies: true,assetsDir: assets, // 静态资源保存…

C. Pinkie Pie Eats Patty-cakes(二分)

Problem - C - Codeforces 小粉饼买了一袋不同馅料的馅饼饼!但并不是所有的馅饼饼在馅料上都各不相同。换句话说&#xff0c;这个袋子里有一些馅料相同的馅饼。小粉派一个接一个地吃蛋糕。她喜欢玩&#xff0c;所以她决定不只是吃馅饼蛋糕&#xff0c;而是尽量不经常吃同样馅料…