MySQL表的进阶知识(下)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: MySQL表的进阶知识

MySQL表的进阶知识(上):https://developer.aliyun.com/article/1520464

四、查询

1、聚合查询

以下演示以school表为基础:

a、聚合函数查询

聚合函数有:


涉及数字的聚合函数遇到null值会进行忽略。

聚合函数查询可以搭配where子句。

例如:统计school表中的元组个数:

select count(*) from school;

例如:求出学校表的姓张的平均工资:

select avg(salary) from school where name like '张%';

例如:求出学校表的最高工资:

select max(salary) from school;

b、group by

分组查询:对表中的数组先进行分组,再进行相关查询。

例如:查询每个职位的最低工资:

select posts, min(salary) from school group by posts;

同样,group by也可以搭配where子句使用:

例如:查询除了院长的职位的最高工资:

select posts, max(salary) from school where posts!="院长" group by(posts);


c、having

having对group by分组之后的结果进行筛选,而where是在group by分组之前进行筛选。  

例如:查询school表中的平均工资在一万以上的:

select posts,avg(salary) from school group by posts having avg(salary)>10000;

2、联合查询

在实际开发中,通常使用的是联合查询,也就是联合多个表进行查询,那么就必须了解表之间的笛卡尔积:

笛卡尔乘积是指在数学中,两个集合XY的笛卡尔积(Cartesian product),又称直积,表示为X × Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员。

那么两个表的笛卡尔积:

a,b两个表进行笛卡尔积,笛卡尔积表的列数=a的列数+b的列数,笛卡尔积表的行数=a的行数*b的行数,

通俗来讲,笛卡尔积就是两个表全排列的所有情况。

后续操作是在以下三个表的基础上:

学生表:


课程表:

分数表:

 

a、内连接

两个表之间的某一字段相等的连接。

语法格式:

select 字段名 from 表1,表2 where 连接条件 and 其他条件;

select 字段名 from 表1 join 表2 on 连接条件 and 其他条件;

连接条件指的是两个表中的相同字段名相等。

例如:查找Java成绩高于85的学生信息 :

select student.name,course.name,score from student,course,score 
where student.id=score.student_id and course.id=score.course_id 
and course.name='Java' and score>85;
--或者
select student.name,course.name,score from student join course join score 
on  student.id=score.student_id and course.id=score.course_id 
and course.name='Java' and score>85;

查询结果:

例如:按照学生的平均分进行排名:

select student.id,student.name,avg(score) as avg from student,course,score 
where student.id=score.student_id and course.id=score.course_id
group by(student.id) order by(avg) desc;
--或者
select student.id,student.name,avg(score) as avg from student join course join score 
on student.id=score.student_id and course.id=score.course_id
group by(student.id) order by(avg) desc;

查询结果:

select student.name,course.name,score from student left join score on student.id=score.student_id left join course on course.id=score.course_id;

b、外连接

通俗来讲,就是把一张表的某些字段连接到另一张表上。

外连接可分为:左外连接和右外连接。

左外连接的语法格式:

select 字段名 from 表1 left join 表2 on 连接条件;

右外连接的语法格式:

select 字段名 from 表1 right join 表2 on连接条件;

例如:查询所有学生的各科成绩:

select student.name,course.name,score from student left join score on
 student.id=score.student_id left join course on course.id=score.course_id;

查询结果:

c、自连接

在同一张表中进行自身的查询,就是将行与行之间的查询转换成列与列之间的查询。

例如:在score表中查询Java成绩高于C++的:

select s1.student_id,s1.course_id,s1.score,s2.course_id,s2.score 
from score as s1,score as s2 where s1.student_id=s2.student_id 
and s1.course_id=12 and s2.course_id=13 and s1.score>s2.score;

查询结果:

3、子查询

是指在select查询语句中再嵌套一个查询语句。

例如:查询选修Java的学生姓名:

select student.name from student where id in
(select student_id from score where course_id=12);

4、合并查询

与 or 关键字查询结果基本一致,只是or是属于单表查询,而合并查询是多表查询。

语法格式:

select …… union select……:对查询结果去重

select …… union all select……:对查询结果不去重

例如:查询选修Java或者分数大于90的学生id:

select student_id from score where course_id=12
union
select student_id from score where score>90;

查询结果:


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
4天前
|
存储 关系型数据库 MySQL
MySQL数据库进阶第六篇(InnoDB引擎架构,事务原理,MVCC)
MySQL数据库进阶第六篇(InnoDB引擎架构,事务原理,MVCC)
|
4天前
|
SQL 关系型数据库 MySQL
MySQL数据库进阶第五篇(锁)
MySQL数据库进阶第五篇(锁)
|
4天前
|
存储 关系型数据库 MySQL
MySQL数据库进阶第三篇(MySQL性能优化)
MySQL数据库进阶第三篇(MySQL性能优化)
|
11天前
|
存储 SQL 关系型数据库
MySQL 进阶使用【函数、索引、视图、存储过程、存储函数、触发器】(2)
MySQL 进阶使用【函数、索引、视图、存储过程、存储函数、触发器】
|
11天前
|
存储 SQL 关系型数据库
MySQL 进阶使用【函数、索引、视图、存储过程、存储函数、触发器】(1)
MySQL 进阶使用【函数、索引、视图、存储过程、存储函数、触发器】
|
13天前
|
分布式计算 DataWorks MaxCompute
DataWorks产品使用合集之需要将mysql 表(有longtext类型字段) 迁移到odps,但odps好像没有对应的类型支持,该怎么办
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
4天前
|
SQL 关系型数据库 MySQL
Python进阶第二篇(Python与MySQL数据库)
Python进阶第二篇(Python与MySQL数据库)
|
4天前
|
存储 SQL 关系型数据库
MySQL数据库进阶第四篇(视图/存储过程/触发器)
MySQL数据库进阶第四篇(视图/存储过程/触发器)
|
4天前
|
SQL 存储 关系型数据库
MySQL数据库进阶第二篇(索引,SQL性能分析,使用规则)
MySQL数据库进阶第二篇(索引,SQL性能分析,使用规则)
|
4天前
|
存储 关系型数据库 MySQL
MySQL数据库进阶第一篇(存储引擎与Linux系统上安装MySQL数据库)
MySQL数据库进阶第一篇(存储引擎与Linux系统上安装MySQL数据库)