MySQL

  • 聚合函数
    • count
    • sum
    • avg
    • max
    • min
    • group by
    • having
  • 联合查询
    • 内连接
    • 外连接
    • 自连接
    • 子查询
      • 单行子查询
      • 多行子查询
        • (not) in
        • (not) exists
    • 合并查询(union)
  • 注意

聚合函数

exam_result表为例

drop table if exists exam_result;
create table exam_result ( id INT, name VARCHAR(20), chinese DECIMAL(3,1), math DECIMAL(3,1), english DECIMAL(3,1)
);
insert into exam_result (id,name, chinese, math, english)
values
(1,'A', 67, 98, 56),
(2,'B', 87.5, 78, 77),
(3,'C', 88, 98, 90),
(4,'D', 82, 84, 67),
(5,'E', 55.5, 85, 45),
(6,'F', 70, 73, 78.5),
(7,'G', 75, 65, 30),
(8,'H', 78, 32, 98);

count

数量

select count(*) from exam_result;

sum

总和(不是数字,无意义)

select sum(math) from exam_result;

avg

平均值

select avg(math) from exam_result;

max

最大值

select max(math) from exam_result;

min

最小值

select min(math) from exam_result;

group by

分组

emp表为例

drop table if exists emp;
create table emp( id int primary key auto_increment, name varchar(20) not null, role varchar(20) not null, salary numeric(11,2)
);
insert into emp(name, role, salary) values
('老师A','讲师', 2000.20),
('老师B','讲师', 2000.99),
('老师C','讲师', 2000.11),
('老师D','教授', 3000.5),
('老师E','辅导员', 1000.33),
('老师F','教授', 3000.66),
('老师E','辅导员', 1000.33);

查询每个角色的最高工资、最低工资和平均工资

将role 相同的分为一组,求其最大,最小,平均值

select role,max(salary),min(salary),avg(salary) from emp group by role;

having

过滤条件

where后不能加聚合函数,需要使用having

查询平均工资低于1500的角色和它的平均工资

select role,avg(salary) from emp group by role having avg(salary)<1500;

联合查询

联合查询就是两张表或者两张以上的表,进行连接查询

因为我们所需要的数据,不仅仅是来自于一张表的,而是来自于多张表的

笛卡尔积

所有的联合查询都是从笛卡尔积当中去取出数据,在取数据的时候,一定是要满足某种规则,接下来,我们来学习一下各种联合查询的方式把~

以下列表为例

drop table if exists classes;
create table classes( id int primary key auto_increment,name varchar(50), `desc` varchar(50)
);
insert into classes(name, `desc`) values
('计算机系2018级1班', '学习了计算机原理'),
('中文系2018级3班','学习了中国传统文学'),
('自动化2018级5班','学习了机械自动化');

drop table if exists student;
create table student( id int primary key auto_increment, sn int,name varchar(30), qq_mail varchar(30), classes_id int
);
insert into student(sn, name, qq_mail, classes_id) values
('10010','张三','123@qq.com',1),
('10012','李四',null,1),
('10034','王五',null,1),
('10023','赵六','456@qq.com',1),
('20019','郑一',null,1),
('20021','刘二','789@qq.com',2),
('30098','lily',null,2),
('30045','joey','happy@qq.com',2);

drop table if exists course;
create table course( id int primary key auto_increment, name varchar(20)
);
insert into course(name) values
('Java'),('中国传统文化'),('计算机原理'),('语文'),('数学'),('英语');

drop table if exists score;
create table score( id int primary key auto_increment, score DECIMAL, student_id int, course_id int
);
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),
(80, 7, 2),(92, 7, 6);


学生有8个
分数有20个
一共8*20=160个

内连接

select 字段 from 表1 别名1 inner join 表2 别名2 on 连接条件 and 其他条件
select 字段 from 表1 别名1,表2 别名2 where 连接条件 and 其他条件
  1. 查询赵六同学的成绩
select stu.sn,stu.name,sco.score,cou.name as 课程名
from student stu
inner join score sco
on stu.id = sco.student_id inner join course cou on sco.course_id = cou.id
and stu.name='赵六';


注:inner可以省略
可以直接用,+where的方式写

select stu.sn,stu.name,sco.score,cou.name as 课程名
from student stu ,score sco,course cou
where stu.id = sco.student_id
and sco.course_id = cou.id
and stu.name='赵六';
  1. 查询所有同学的总成绩
select stu.sn,stu.name,sum(score)
from student stu,score sco
where stu.id = sco.student_id
group by stu.id;

外连接


查询每个同学的成绩,个人信息,若无成绩也要显示

select stu.id,stu.name,sco.score
from student stu,score sco
where stu.id=sco.student_id
group by stu.id;
select stu.id,stu.name,sco.score
from student stu left join score sco
on stu.id=sco.student_id
group by stu.id;
select stu.id,stu.name,sco.score
from score sco right join student stu
on stu.id=sco.student_id
group by stu.id;

自连接

把一张表看做两张表来使用

查询所有“计算机原理”成绩比“java”成绩高的信息

select s2.* from score s1,score s2
where s1.student_id = s2.student_id
and s1.score<s2.score
and s1.course_id = 1
and s2.course_id = 3;

子查询

单行子查询

返回一行记录的子查询

查询“郑一”同学的同班同学

①先查询他是哪个班的

select classes_id from student stu where name = '郑一';


②再查这个班有谁

select * from student where classes_id = 1;


③合并

select * from student
where classes_id = (select classes_id from student stu where stu.name = '郑一'
);

多行子查询

返回多行记录的子查询

(not) in

查询语文或英语的成绩信息
①先查语文或英语的id

select id from course
where name = '语文' or name = '英语';

②再查这两个id对应的成绩

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

(not) exists

只要这个表达式为真,返回true

select * from A where exists (select 1 from B where B.id=A.id
);

①首先执行一次外部查询,并缓存结果集,如 select * from A
②遍历外部查询结果集的每一行记录为R,代入子查询中作为条件进行查询,如 select 1 from B where B.id=A.id
③若子查询有返回结果,exists子句返回true,这一行R可以作为外部查询结果行,否则不可

合并查询(union)

union :合并且去重

select * from student
where id <= 3
union  select * from studentwhere name = '张三';

union all :合并(不去重)

select * from student
where id <= 3
union all select * from studentwhere name = '张三';

注意

禁止三张表以上的连表查询,若需要三张表,在后端代码中解决,不要在MySQL中解决

MySQL Ⅳ 查询进阶相关推荐

  1. MySQL查询进阶之多表查询

    一.多表查询 1.引出 2.笛卡尔积 3. 笛卡尔积的解决方法 二.多表查询分类 1.等值连接和非等值连接 2.自连接和非自连接 3.内连接和外连接 SQL92:使用(+)创建连接 SQL99语法实现 ...

  2. mysql查询进阶——员工表与部门表连接查询

    如下是员工表与部门表的表内容. 查询月薪最高的员工姓名和月薪(子查询) select ename, sal from tb_emp where sal = (select max(sal) from ...

  3. mysql可以存储标点么_MySQL查询(进阶)(每个标点都是重点)

    MySQL 是工作中很普遍的需要用到的,所以必须掌握,而 之前我们一直说的都是怎么存. 你只会存不会取有个屁用.所以希望大家在如何查询读取数据这方面多下点功夫. 这篇和上一篇都是干货,我也是第一次学. ...

  4. MySQL表的查询进阶

    目录 MySQL表的设计 查询进阶版本 插入查询结果-->属于新增 聚合查询 group by子句 having子句 联合查询 外连接 对比外连接和内连接: 自连接 子查询 合并查询 MySQL ...

  5. MySQL的进阶查询

    MySQL进阶语句 一.MySQL进阶查询 1.为什么要用? 对MySQL数据库的查询,有时候需要对查询的结果集进行处理,包含排序或分组等等. 2.常用的查询和语法结构 2.1按字段排序 使用oede ...

  6. MySQL的进阶实战篇

    关联文章: MySQL的初次见面礼基础实战篇 MySQL的进阶实战篇 本篇上一篇博文MySQL的初次见面礼基础实战篇的延续,是mysql的进阶内容的记录,本篇主要知识点如下: 进阶实战篇 进阶实战篇 ...

  7. mysql 连接查询_Swoole 实战:MySQL 查询器的实现(协程连接池)

    Swoole 实战:MySQL 查询器的实现(协程连接池) 需求分析 本篇我们将通过 Swoole 实现一个自带连接池的 MySQL 查询器: 1. 支持通过链式调用构造并执行 SQL 语句: 2. ...

  8. mysql 查询语句_MySQL相关(一)- 一条查询语句是如何执行的

    前言 学习一个新知识最好的方式就是上官网,所以我先把官网贴出来 MySQL官网 (点击查阅),如果大家有想了解我没有说到的东西可以直接上官网看哈~目前 MySQL 最新大版本为8.0,但是鉴于目前应用 ...

  9. 《大型数据库技术》MySQL的进阶开发技巧

    MySQL的进阶开发技巧 1.MySQL的存储过程 1.1 创建企业销售系统的数据库,命名为salesdb 1.2 创建一张商品销售表,命名为salerecords,包括如下字段:商品ID,商品名称, ...

最新文章

  1. bootstrap的两种在input框后面增加一个图标的方式
  2. 【笔记】Hexo+Github博客网站搭建,初试环境搭建及Matery主题配置感受
  3. 第4章 最基础的分类算法-k近邻算法
  4. SAP 系统中图标代码的查找
  5. 存储器芯片巨头动态观察:三星、美光、SK海力士都在做什么?
  6. 大数据之-Hadoop环境搭建_hadoop官网手册---大数据之hadoop工作笔记0020
  7. 简述直方图和柱形图的区别_如何区分直方图与柱形图
  8. Myeclipse中web project 与java project区别
  9. java用户注册模块_用户注册登录模块设计方案报告.docx
  10. 网络安全应急响应-常用工具
  11. 移动计算机笔试题,广东移动笔试题目
  12. ScienceWord控件问题以及IE8对于base64编码图片长度限制的解决方案
  13. 电子病历基本数据集_电子病历质控的三项是因素-病历质控系统助力提升电子病历质量...
  14. leetcode 5724. 绝对差值和
  15. 阻止第三方 cookie_如何在每个Web浏览器中阻止第三方Cookie
  16. Windows 2000/XP IIS5.1安装
  17. Linux下service xxx start/stop/restart启动服务、关闭服务、重启服务深入理解@
  18. xubuntu系统关闭自动锁屏和待机
  19. Spring常用设计模式--简单工厂模式
  20. go redis incr的使用

热门文章

  1. 腾讯QQ圈子——数据挖掘的典型
  2. ath10k 出现ath10k_htt_t2h_msg_handler+0xebc/0x1efc解决方案
  3. 素描滤镜_您可能不知道的素描技巧和键盘快捷键
  4. 第四代反应堆的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  5. Qt中多线程的一种使用
  6. 浮动带来的问题,以及清除浮动影响的方式
  7. 【BYM】Android 实现相机快门动画
  8. 【BYM】Android 实现相机快门动画,hashmap底层实现原理
  9. DeDeCMS v5.7 SP2正式版 前台任意用户密码修改 漏洞复现
  10. 基于docker+reveal.js搭建一个属于自己的在线ppt网站