MySQL基础篇的补充

前言: 

查询语句的书写顺序

select ===> from ===> where ===> group by ===> having ===> order by ===> limit

查询语句的执行顺序

from ===> where ===> group by ===> having ===> select ===> order by ===> limit

这个很重要,提前再复习一下。

这里如果有一些基础,但是语句不是很熟悉的同学,可以去看另一位大佬的博客:MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)_mysql有这一篇幅就够了-CSDN博客

本篇在其基础上做了一些补充(作者抄了一遍大佬的博客,但还未经博主同意所以没有发布)。希望各位谅解。 

一、分组查询:

在此之前我们先准备数据:

drop table if exists emp;create table emp (id int primary key auto_increment,name varchar(20),role varchar(20),salary int
);insert into emp values(null, '张三', '程序员',10000), (null, '李四', '程序员', 1100), (null, '王五', '程序员', 1200);insert into emp values(null, '赵六', '产品经理', 9000), (null, '田七','产品经理', 9500),(null, '周八', '老板',100000);

此时我们直接进行分组查询:

select role, salary from emp group by role;

运行结果及分析:

所以分组查询往往还是要搭配聚合函数使用的,否则这里的查询结果就没有意义。

使用group by的时候,还可以搭配条件,需要区分清楚,该条件是分组之前的条件,还是分组之后的条件。

1. 搭配 where 使用 

查询每个岗位的平均薪资,但排除张三

select role, avg(salary) from emp where name != '张三' group by role;

运行结果: 

2. 搭配 having 使用

查询每个岗位的平均薪资,但是排除平均薪资超过2w的结果

select role, avg(salary) from emp group by role having avg(salary) > 20000;

运行结果:

二、联合查询(多表查询)

 我们先准备数据:

drop table if exists classes;
drop table if exists student;
drop table if exists course;
drop table if exists score;create table classes (id int primary key auto_increment,name varchar(20),`desc` varchar(100)
);create table student (id int primary key auto_increment,sn varchar(20),name varchar(20),qq_mail varchar(20),classes_id int
);create table course (id int primary key auto_increment,name varchar(20)
);create table score (score decimal(3,1),student_id int,course_id int
);insert into classes(name, `desc`) values
('计算机系2019级1班', '学习了计算机原理、C和Java语言、数据结构和算法'),
('中文系2019级3班','学习了中国传统文学'),
('自动化2019级5班','学习了机械自动化');
insert into student(sn, name, qq_mail, classes_id) values
('09982','黑旋风李逵','xuanfeng@qq.com',1),
('00835','菩提老祖',null,1),
('00391','白素贞',null,1),
('00031','许仙','xuxian@qq.com',1),
('00054','不想毕业',null,1),
('51234','好好说话','say@qq.com',2),
('83223','tellme',null,2),
('09527','老外学中文','foreigner@qq.com',2);
insert into course(name) values
('Java'),('中国传统文化'),('计算机原理'),('语文'),('高阶数学'),('英文');
insert into score(score, student_id, course_id) values
-- 黑旋风李逵
(70.5, 1, 1),(98.5, 1, 3),(33, 1, 5),(98, 1, 6),
-- 菩提老祖
(60, 2, 1),(59.5, 2, 5),
-- 白素贞
(33, 3, 1),(68, 3, 3),(99, 3, 5),
-- 许仙
(67, 4, 1),(23, 4, 3),(56, 4, 5),(72, 4, 6),
-- 不想毕业
(81, 5, 1),(37, 5, 5),
-- 好好说话
(56, 6, 2),(43, 6, 4),(79, 6, 6),
-- tellme
(80, 7, 2),(92, 7, 6);

 

此时要查询"许仙"的各科成绩:

我们先分析条件:"许仙"是学生表(student)中,而成绩是成绩表(score)中的,所以我们要联合这两张表,也就是将这两张表进行笛卡尔积;之后需要筛选条件,也就是学生表中的id对应成绩表中的id。

select * from student, score where id = student_id;

运行结果:

此时我们可以精简列,之后再加上学生表中姓名为"许仙"同学的条件即可。

select student.name, score.score from student, score where student.id = score.student_id and student.name = '许仙';

1. 基于多表查询和聚合查询的综合使用: 

查询所有同学的总成绩及同学的个人信息:

# select * from student, score where student.id = score.student_id;
# select * from student, score where student.id = score.student_id group by student.name;select student.name, sum(score.score) from student, score where student.id = score.student_id group by student.name;

运行结果:

2. 3张表的联合查询

列出每个同学的每门课程课程名字和分数:

这里会用到3张表,学生表、分数表、课程表。我们可以先利用两张表得到结果,之后再逐步筛选最终结果:

# select * from student, course, score where student.id = score.student_id;# select * from student, course, score where student.id = score.student_id and score.course_id = course.id;# select student.name, course.name, score.score from student, course, score where student.id = score.student_id and score.course_id = course.id;# 此时列名含义模糊,我们可以起别名select student.id, student.name as student_name, course.name as course_name, score.score from student, course, score where student.id = score.student
_id and score.course_id = course.id;

运行结果:

三、内连接和外连接 

上述操作都是内连接,对MySQL而言,进行多表查询还可以使用"外连接"。

如果这两个表里面的记录都是存在对应关系的,内连接和外连接的结果都是一致的;如果存在不对应关系,内连接和外连接就会出现差别。

此时我们先准备数据:

drop table if exists student;
drop table if exists score;create table student (id int,name varchar(20)
);create table score (id int,score int
);insert into student values (1, '张三'), (2, '李四'), (3, '王五');insert into score values (1, 90), (2, 80), (4, 70);

注意:此时两张表中的内容并没有一一对应,若一一对应使用内外连接结果都是一样的。

此时就可以发现student表中没有4号同学,而score表中没有3号同学成绩。

1. 内连接 

和之前的多表查询一样:

查询学生和学生成绩: 

select * from student, score where student.id = score.id;

运行结果:

2. 外连接 

我们也是和内连接做相同操作,但是语法格式不一样:

select * from student join score on student.id = score.id;# 以下方式与上述方式等价
select * from student inner join score on student.id = score.id;# inner 可以省略

运行结果:

这就是外连接,join 代表逗号,where 换成 on,目前这两种写法等价。 

2.1 左外连接

左外连接使用left表示:

select * from student left join score on student.id = score.id;

运行结果:

左外连接:以左侧表为基准,保证左侧表每个数据都会出现在最终结果中,若在右侧表中不存在,对应的列就会替换为null。

2.2 右外连接

右外连接使用right表示:

select * from student right join score on student.id = score.id;

运行结果:

右外连接:以右侧表为基准,保证右侧表每个数据都会出现在最终结果中,若在左侧表中不存在,对应的列就会替换为null。

2.3 总结外连接

如下图:

其实有全外连接,但是MySQL不支持。

四、自连接:

一张表,自己和自己进行笛卡尔积。

有的时候,我们需要进行 行和行 之间的比较,而sql只能进行 列和列 之间的比较。有的时候,可能会涉及到行和行比较的需求,就可以使用自连接,把 行关系 转换为 列关系。

数据准备:

drop table if exists classes;
drop table if exists student;
drop table if exists course;
drop table if exists score;create table classes (id int primary key auto_increment,name varchar(20),`desc` varchar(100)
);create table student (id int primary key auto_increment,sn varchar(20),name varchar(20),qq_mail varchar(20),classes_id int
);create table course (id int primary key auto_increment,name varchar(20)
);create table score (score decimal(3,1),student_id int,course_id int
);insert into classes(name, `desc`) values
('计算机系2019级1班', '学习了计算机原理、C和Java语言、数据结构和算法'),
('中文系2019级3班','学习了中国传统文学'),
('自动化2019级5班','学习了机械自动化');
insert into student(sn, name, qq_mail, classes_id) values
('09982','黑旋风李逵','xuanfeng@qq.com',1),
('00835','菩提老祖',null,1),
('00391','白素贞',null,1),
('00031','许仙','xuxian@qq.com',1),
('00054','不想毕业',null,1),
('51234','好好说话','say@qq.com',2),
('83223','tellme',null,2),
('09527','老外学中文','foreigner@qq.com',2);
insert into course(name) values
('Java'),('中国传统文化'),('计算机原理'),('语文'),('高阶数学'),('英文');
insert into score(score, student_id, course_id) values
-- 黑旋风李逵
(70.5, 1, 1),(98.5, 1, 3),(33, 1, 5),(98, 1, 6),
-- 菩提老祖
(60, 2, 1),(59.5, 2, 5),
-- 白素贞
(33, 3, 1),(68, 3, 3),(99, 3, 5),
-- 许仙
(67, 4, 1),(23, 4, 3),(56, 4, 5),(72, 4, 6),
-- 不想毕业
(81, 5, 1),(37, 5, 5),
-- 好好说话
(56, 6, 2),(43, 6, 4),(79, 6, 6),
-- tellme
(80, 7, 2),(92, 7, 6);

显示所有"计算机原理"成绩比"Java"成绩高的同学成绩信息:

根据观察:也就是找课程3的成绩比课程1的成绩更高的同学信息(course id 3 比 course id 1 成绩高的同学)。

此时我们直接进行自连接:

select * from score, score;

运行结果:

 

可以发现报错,此时要起一个别名。

select * from score as s1, score as s2;

此时我们关注学生信息,所以可以确定筛选条件:

select * from score as s1, score as s2 where s1.student_id = s2.student_id;

运行结果:

此时就可以确定最终查询语句:

# select * from score as s1, score as s2 where s1.student_id = s2.student_id and s1.course_id = 3 and s2.course_id
= 1 and s1.score > s2.score;# 最后精简一下列
select s1.student_id, s1.score as F3, s2.score as F1 from score as s1, score as s2 where s1.student_id = s2.stude
nt_id and s1.course_id = 3 and s2.course_id = 1 and s1.score > s2.score;

运行结果:

如果此时想要学生名字,就可以拿这个表和学生表做笛卡尔积;如果此时想要课程名字,就可以拿这个表和课程表做笛卡尔积。

五、子查询

这个在前面提到的文章讲到过,我们大概说一下,因为用处很小。

我们可以通过这两句SQL来完成,当然也可以通过合并SQL语句完成,也就是子查询。

1. 单行子查询 

只返回一行查询记录。

查询与"不想毕业"同学的同班同学:

# 先利用第一个查询语句缩小范围
select classes_id from student where name = '不想毕业';# 之后利用其结果确定最终结果
select name from student where classes_id = 1 and name != '不想毕业';

运行结果:

通过合并SQL语句来完成:

select name from student where classes_id = (select classes_id from student where name = '不想毕业') and name != '不想毕业';

2. 多行子查询

返回多行记录的子查询。尝尝搭配in使用。

查询"语文"或"英文"课程的成绩信息:

应该在course和score中进行查询,联合查询也是没有问题的。这里先展示联合查询:

select * from course, score where course.id = score.course_id and (course.name = '英文' or course.name = '语文');

运行结果:

分步查询步骤为:

1.先通过课程名字找到课程id
2.再通过课程id在分数表中进行查询

select id from course where name = '语文' or name = '英文';
select score.student_id, score.course_id, score.score from score where score.course_id in (4, 6);

运行结果:

子查询使用in关键字:

select score.student_id, score.course_id, score.score from score where score.course_id in (select id from course where name = '语文' or name = '英文');

运行结果:

六、合并查询(union关键字)

 把多个sql查询结果集合合并到一起成为合并查询。这里我们主要了解union关键字。

查询id小于3,或者名字为"英文"的课程:

select * from course where id < 3 or name = '英文';# 等价于select * from course where id < 3 union select * from course where name = '英文';

运行结果:

union可以充当or的作用,但是union允许把两个不同的表合并在一起。

合并的两个sql结果集的列,需要匹配。

这里我们在准备其他的表:

drop table if exists score;drop table if exists student;create table student1(id int,name varchar(20)
);create table student2(studentId int,studentName varchar(20)
);insert into student1 values(1, '张三'), (2, '李四');insert into student2 values(1, '张三'), (3, '王五');

查询两张表的学生信息:

select * from student1 union select * from student2;

运行结果:

可以发现:使用union进行合并的时候,是会对结果进行去重的,若不想去重,可以使用union all。 

select * from student1 union all select * from student2;

运行结果:

总结:

查询语句的书写顺序

select ===> from ===> where ===> group by ===> having ===> order by ===> limit

查询语句的执行顺序

from ===> where ===> group by ===> having ===> select ===> order by ===> limit

重要的事情说三遍,虽然这些东西可能在实际上运用的很少,但是我们也必须了解,毕竟技多不压身。 

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

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

vue入门小练习

文章目录 1.案例需求2.编程思路3.案例源码4.小结 1.案例需求 一个简易的计算器&#xff0c;其效果如下&#xff1a; 图片切换&#xff0c;其效果如下&#xff1a; 简易记事本&#xff0c;其效果如下&#xff1a; 2.编程思路 1.这个Vue.js应用实现了一个简单的计算器&#x…

检索索引对象中的重复值、删除重复值pandas.Index.duplicated

【小白从小学Python、C、Java】 【考研初试复试毕业设计】 【Python基础AI数据分析】 检索索引对象中的重复值、删除重复值 pandas.Index.duplicated [太阳]选择题 根据代码&#xff0c;下列哪个选项正确表示了去重后的结果&#xff1f; import pandas as pd idx pd.Index([1,…

图书管理系统小程序的设计

管理员账户功能包括&#xff1a;系统首页&#xff0c;个人中心&#xff0c;用户管理&#xff0c;图书分类管理&#xff0c;图书信息管理&#xff0c;我的待还处管理&#xff0c;图书归还管理&#xff0c;催还提醒管理&#xff0c;系统管理 微信端账号功能包括&#xff1a;系统…

如何实现接口幂等性?

概述 幂等&#xff0c;英文idempotent [aɪdempətənt] 幂等这个词源自数学&#xff0c;是数学中的一个概念&#xff0c;常见于抽象代数中&#xff0c;表达的是N次变换与1次变换的结果相同&#xff0c;在计算机的各个领域都借用了该概念 幂等函数或幂等方法&#xff0c;是指…

stm32单片机个人学习笔记6(EXTI外部中断)

前言 本篇文章属于stm32单片机&#xff08;以下简称单片机&#xff09;的学习笔记&#xff0c;来源于B站教学视频。下面是这位up主的视频链接。本文为个人学习笔记&#xff0c;只能做参考&#xff0c;细节方面建议观看视频&#xff0c;肯定受益匪浅。 STM32入门教程-2023版 细…

大模型Qwen2.5 家族的最新成员

阿里今天发布了 Qwen 家族的最新成员&#xff1a;Qwen2.5&#xff0c;包括语言模型 Qwen2.5&#xff0c;以及专门针对编程的 Qwen2.5-Coder 和数学的 Qwen2.5-Math 模型。 所有开放权重的模型都是稠密的、decoder-only 的语言模型&#xff0c;提供多种不同规模的版本&#xff…

某准网爬虫逆向

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、目标网站二、抓包分析 1.数据包2.逆向过程总结 前言 提示&#xff1a;这里可以添加本文要记录的大概内容&#xff1a; 例如&#xff1a;随着人工智能的不…

AI 时代的网络危机沟通计划

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

ICM20948 DMP代码详解(35)

接前一篇文章&#xff1a;ICM20948 DMP代码详解&#xff08;34&#xff09; 上一回终于解析完了inv_icm20948_initialize_lower_driver函数&#xff0c;本回回到icm20948_sensor_setup函数&#xff0c;继续往下进行解析。为了便于理解和回顾&#xff0c;再次贴出icm20948_senso…

OpenLayers 开源的Web GIS引擎 - 地图初始化

在线引用&#xff1a; 地址&#xff1a;OpenLayers - Get the Code 离线引用&#xff1a; 下载地址&#xff1a;Releases openlayers/openlayers GitHub v10.0.0版本 地图初始化代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><…

以STM32CubeMX创建DSP库工程方法二

以Keil创建DSP库工程方法二 Keil 中宏定义的添加 编译后直接报错高达420项&#xff0c;摘取一部分错误信息下来如下&#xff1a; D:\AppData\Local\Arm\Packs\ARM\CMSIS\5.7.0\CMSIS\DSP\Include\arm_math.h(6911): error: #757: function “int32_t” is not a type name 以…

【开源免费】基于SpringBoot+Vue.JS图书馆管理系统(JAVA毕业设计)

本文项目编号 T 044 &#xff0c;文末自助获取源码 \color{red}{T044&#xff0c;文末自助获取源码} T044&#xff0c;文末自助获取源码 目录 一、系统介绍二、演示录屏三、启动教程四、功能截图五、文案资料5.1 选题背景5.2 国内外研究现状5.3 可行性分析5.4 用例设计 六、核…

数据结构:二叉树(一)

ps&#xff1a;偷懒了几天&#xff0c;接着更新 树的概念 树是一种非线性的数据结构&#xff0c;它是由n&#xff08;n>0&#xff09;个有限结点组成一个具有层次关系的集合。 把它叫做树是因为它看起来像一棵倒挂的树&#xff0c;也就是说它是根朝上&#xff0c;而叶朝下的…

银河麒麟高级服务器操作系统V10:提升普通用户操作权限

银河麒麟高级服务器操作系统V10&#xff1a;提升普通用户操作权限 1. 打开终端2. 切换到root用户&#xff08;可选&#xff09;3. 将用户加入到wheel组4. 验证用户组变更5. 使用sudo执行命令结论 &#x1f496;The Begin&#x1f496;点点关注&#xff0c;收藏不迷路&#x1f4…

利用人工智能改变视频智能

人工智能视频分析正在将安全摄像头变成强大的传感器&#xff0c;可以改善您监控站点安全的方式。借助人工智能 (AI)&#xff0c;摄像头可以独立准确地检测威胁&#xff0c;而无需人工不断观看视频。 这并不奇怪——过去几年&#xff0c;这一直是安全行业协会 (SIA) 提出的几大…

软考高级:数据库关系模式推理规则 AI 解读

你提出的是关系模式中的一些经典推理规则&#xff0c;这些规则在数据库理论、函数依赖和范式相关的讨论中经常出现。我们可以通过以下方式深入理解这些规则&#xff0c;并且对其中的推理逻辑进行分解。 生活化例子 想象你在管理一家快递公司&#xff0c;货物需要从仓库&#…

低版本SqlSugar的where条件中使用可空类型报语法错误

SQLServer数据表中有两列可空列&#xff0c;均为数值类型&#xff0c;同时在数据库中录入测试数据&#xff0c;Age和Height列均部分有值。   使用SqlSugar的DbFirst功能生成数据库表类&#xff0c;其中Age、Height属性均为可空类型。   开始使用的SqlSugar版本较低&…

传奇外网架设全套图文教程-BLUE引擎

提示&#xff1a; 当你拿到一个BLUE引擎的版本&#xff0c;首先查看一下版本内文件是否完整&#xff0c;一个完整的BLUE版本包括&#xff1a;DBServer、LoginGate、LoginSrv、LogServer、Mir200、Mud2、RunGate、SelGate、网站和GameCenter.exe&#xff08;引擎&#xff09;&am…

群晖套娃:群晖+飞牛fnOS二合一,群晖nas安装飞牛fnOS系统实录(飞牛fnOS初体验,如何挂载网盘视频,轻松实现影视刮削)

文章目录 📖 介绍 📖🏡 演示环境 🏡📒 飞牛fnOS 📒📝 什么是飞牛fnOS?📝 准备工作📝 安装飞牛fnOS📝 影视刮削⚓️ 相关链接 ⚓️📖 介绍 📖 最近有一款很火的国产NAS系统吸引了不少用户的注意。你是否曾想过,将这种新兴系统安装到你的群晖设备上,实…

LLMs之MemLong:《MemLong: Memory-Augmented Retrieval for Long Text Modeling》翻译与解读

LLMs之MemLong&#xff1a;《MemLong: Memory-Augmented Retrieval for Long Text Modeling》翻译与解读 导读&#xff1a;MemLong 是一种新颖高效的解决 LLM 长文本处理难题的方法&#xff0c;它通过外部检索器获取历史信息&#xff0c;并将其与模型的内部检索过程相结合&…