mysql基础(3)-高级查询

浏览: 1013

image.png

聚合函数

count 返回查询结果的条数

max 返回查询结果的最大值

min 返回查询结果的最小值

sum 返回查询结果的和

avg 返回查询结果的平均值

 

统计分数大于等于90的人数:

mysql> select count(*) from new_student

        -> where score >="90"; 

image.png

使用distinct剔除字段值重复的条数

mysql> select count(distinct score) from new_student

        -> where score >="90";

image.png

统计最高分-max

mysql> select max(score) from new_student;

image.png

统计最低分-min

mysql> select min(score) from new_student;

 mysql> select min(score) from new_student

        -> where score >=60;

image.png

统计分数大于等于90的分数的和-sum

 mysql> select sum(score) from new_student

        -> where score >="90";

image.png

统计平均数-avg

 mysql> select avg(score) from new_student

        -> where score >="80";

image.png

分组查询

语法格式;

select [聚合函数] 字段名 from 表名

where 查询条件

group by 字段名

having 过滤条件

 

mysql> select score,count(*) from new_student

        -> where score >=80

        -> group by score;

image.png

mysql> select score,count(*) from new_student

        -> where score >=80             

        -> group by score

        -> having score >=90;

注:having子语句与where子语句区别:前者在分组后对记录进行过滤,后者在分组前对记录进行过滤

image.png

mysql> select score,count(*) from new_student        -> where score >=80

        -> group by score

        -> having score >=90

        -> order by score desc;

image.png

联合查询

语法格式

select 语句

union [all]

select 语句

...

 注:联合查询结果使用第一个select语句中的字段名

 

mysql> select * from test_wl

        -> union

        -> select * from test_wu;

image.png

推荐 0
本文由 邬家栋 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册