当前位置:   article > 正文

MySQL概述三(聚合函数+日期时间函数+数学函数+limit子句+group by分组+子查询)_group by 时间函数

group by 时间函数

一(聚合函数)

1.avg() 返回某字段的平均值

eg:查询英雄的平均等级  (别名可加可不加)

select avg(level)  '平均等级' from t_hero;

2.count() 统计某字段的行数

eg:查询一共有多少个英雄

select count(*) '英雄的数量' from t_hero;

3.max() 返回某字段的最大值

eg:查询出最高的成长值

select max(growth) from t_hero;

4.min() 返回某字段的最小值

eg:查询最低的成长值

select min(growth) from t_hero;

5.sum() 返回某字段的和

eg:统计出英雄等级的总和

select sum(level) from  t_hero;

二(日期时间函数)

6.获取当前日期:select curdate();

7.获取当前的时间:select curtime();

8.获取当前日期和时间:select now();

9.查询指定日期是一年中的第几周:select week('2022-01-10');

10.查询当前时间是一年中的第几周:select week(now());

11.查询指定日期的年份:select year('2022-10-11');

12.查询当前时间的年份:select year(now());

13.查询指定时间的小时值:select hour('20:39:45');

14.查询指定的分钟值:select minute('20:39:45');

15.查询date1和date2日期之间间隔的天数:datediff(date1,date2)
eg:select datediff('2022-01-04','2022-01-10');
eg:计算还有多少天过生日?
select datediff('2022-07-22',now());

16.查询日期参数date加上n天后的日期: adddate(date,n)
eg:select adddate('2022-01-03',7);
eg:select adddate(now(),7);
eg:select adddate(now(),-7);

三(数学函数) 

17.ceil(x) 返回大于或者等于数值x的最小整数 (向上取整)
select ceil(2.3);3
select ceil(2.8);3

18. floor(x) 返回小于或等于数值x的最大整数(向下取整)
select floor(2.3);2
select floor(2.8);2

19.round(x) 返回最接近数值x的整数(四舍五入)
select round(2.3);2
select round(2.8);3

20.round(x,y) 返回按照四舍五入的方式保留y为小数的值
select round(3.1415926,2);3.14
select round(3.1465926,2);3.15

21. truncate(x,y) 将x按照保留y位小数的方式,直接截取
select truncate(3.1415926,2);3.14
select truncate(3.1465926,2);3.14

22.rand() 返回0-1 之间的随机数
select rand();
eg:返回0-10之间的随机数   select ceil(rand()*10);

四(limit)

limit 子句,用来限制结果集  (做分页时可使用)
格式:limit 位置偏移量(从0开始,和数组下标类似) , 显示的行数

eg:查询前4个英雄
 

select * from t_hero limit 0, 4;  

当数据从第一条数据开始是,0 可以省略不写

select * from t_hero limit 4;


eg:查询从第4个英雄开始的后5个英雄数据

select * from t_hero limit 3,5;

eg: 每页显示4条数据,查看第3页的数据

select * from t_hero limit 8,4;

eg:查询前4个法师英雄

select * from t_hero where classId=103 limit 4;

总结:编写sql语句的顺序
select <字段列表>
from <表或者视图>
where <查询条件>
group by <分组字段名>
order by <排序>
limit 限制


五(子查询)

问题: 查询等级最高的英雄有哪些?
方式一: 分步查询
1. 查询出最高等级为多少?
select max(level) from t_hero; 查出来最高等级是15
2. 查询出哪些英雄的等级为15级
select * from t_hero where level = 15;


方式二: 子查询
子查询是一个嵌套在select,insert,update或者delete语句或其他语句中的查询语句
子查询在where语句中的一般用法
select ... from 表 where 字段 比较运算符 (子查询);
eg:查询等级最高的英雄有哪些?

select * from t_hero where level = (select max(level) from t_hero);
子查询和等号联合使用的时候,必须保证子查询的返回值不能多于一个
错误写法:select * from t_hero where level = (select max(level),min(level) from t_hero);


eg:查询和东皇太一属于同一种类型的英雄

方式一: 分步查询
1. 查询出东皇太一的类型
select classId from t_hero where name='东皇太一';
2. 查询出英雄类型为101,或者106 的英雄
select * from t_hero where classId=101 or classId=106;
select * from t_hero where classId in (101,106);

方式二:子查询
错误写法:select * from t_hero where classId = (select classId from t_hero where name='东皇太一');
正确写法:select * from t_hero where classId in (select classId from t_hero where name='东皇太一');

子查询的查询语句,只能查询出一列结果
注意: 我们常用in 替换等号,in后面的子查询可以返回多行记录

eg:查询出最新上架的英雄

分步查询:
1. 查询最新上架的时间
select max(addTime) from t_hero;
2. 查询出哪个英雄在2020-12-06 上架的
select * from t_hero where addTime in ('2020-12-06');
子查询:
select * from t_hero where addTime in (select max(addTime) from t_hero);


六(分组: group by)

问题:要查询出每个类型的英雄的平均等级

group by 字段  表示:根据指定的字段去将数据分类

select classId ,avg(level) from  t_hero  group by classId;

eg:要查询出每个类型的英雄的平均等级,根据等级从高到低排序

select classId ,avg(level) from  t_hero  group by classId  order by avg(level) desc;

eg:要查询出每个类型的英雄的平均等级,显示大于平均等级大于7 的数据
错误写法:select classId ,avg(level) from  t_hero where avg(level)>7  group by classId;

注意:针对分组之后的查询结果需要进一步筛选,此时不能使用where,需要使用having。

正确写法:select classId ,avg(level) from  t_hero  group by classId  having avg(level)>7 ;

 总结:
where子句:用来筛选from子句中指定的操作产生的行
group by :用来对where子句查询出来的结果进行分组
having子句: 用来从分组之后的结果中筛选出行

多列分组
eg:分别统计,每种英雄,每个等级的英雄数量

select classId '英雄类型' ,level '等级',count(level) from t_hero group by classId,level; 

多列分组时,要主要分析出优先根据谁分组,优先分组的字段优先写在group by 后面。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/441353
推荐阅读
相关标签
  

闽ICP备14008679号