MySql: Year, Quarter, Month, Day, Hour statistics
-- 统计 select count(*) as '当天记录数' from web_product where date(p_createtime) = curdate();
select count(*) as '当天记录数' from web_product where to_days(p_createtime) = to_days(now()); SELECT count(*) as '昨天记录数' FROM web_product WHERE TO_DAYS( NOW( ) ) - TO_DAYS( p_createtime) <= 1; -- 前一天
select count(*) as '前一天记录数' from web_product where date(p_createtime) = date_sub(curdate(),interval 1 day); select count(*) as '本周记录数' from web_product where date(p_createtime) >= date_sub(curdate(),interval 7 day)
and date(p_createtime) <= date_sub(curdate(),interval 1 day); SELECT count(*) as '7天的记录数' FROM web_product where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(p_createtime); -- 查询近30天的记录
SELECT * FROM web_product where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(p_createtime); -- 查询本月的记录
SELECT * FROM web_product WHERE DATE_FORMAT(p_createtime,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m'); -- 查询上一月的记录
SELECT * FROM web_product WHERE PERIOD_DIFF(date_format(now(),'%Y%m'),date_format(p_createtime,'%Y%m'))=1; -- 查询本季度数据
select * from web_product where QUARTER(p_createtime)=QUARTER(now()); -- 查询上季度数据
select * from web_product where QUARTER(p_createtime)=QUARTER(DATE_SUB(now(),interval 1 QUARTER)); -- 查询本年数据
select * from web_product where YEAR(p_createtime)=YEAR(NOW()); -- 查询上年数据
select * from web_product where year(p_createtime)=year(date_sub(now(),interval 1 year)); -- 查询当前这周的数据
SELECT * FROM web_product WHERE YEARWEEK(date_format(p_createtime,'%Y-%m-%d')) = YEARWEEK(now()); -- 查询上周的数据
SELECT * FROM web_product WHERE YEARWEEK(date_format(p_createtime,'%Y-%m-%d')) = YEARWEEK(now())-1; -- 查询当前月份的数据
select * from web_product where date_format(p_createtime,'%Y-%m')=date_format(now(),'%Y-%m');
-- 查询距离当前现在6个月的数据
select p_name,p_createtime from web_product where p_createtime between date_sub(now(),interval 6 month) and now(); -- 按年汇总,统计: select sum(mymoney) as totalmoney, count(*) as sheets from web_product group by date_format(p_createtime, '%Y'); select date_format(p_createtime, '%Y') as 'year',count(*) as sheets from web_product group by date_format(p_createtime, '%Y'); select DATE_FORMAT(p_createtime,'%Y') years,sum(duration) dur from web_product tv where 1=1 GROUP BY years ORDER BY years desc; select DATE_FORMAT(p_createtime,'%Y') years,count(*) as sheets from web_product where 1=1 GROUP BY years ORDER BY years desc; SELECT DATE_FORMAT(p_createtime,'%Y') years,COUNT(*) COUNT FROM web_product GROUP BY years; SELECT year(p_createtime) as 'yearname',count(*) as'sheet' FROM `web_product` group by yearname; SELECT count(*), year(p_createtime) yearname FROM `web_product` group by yearname; SELECT year(p_createtime) yearname FROM `web_product`; SELECT DISTINCT(year(p_createtime)) yearname FROM `web_product`; SELECT COUNT(DISTINCT(year(p_createtime))) yearname FROM `web_product`; SELECT year(Addtime) as 'yearname',count(*) as'sheet' FROM `duwebstat` group by yearname; SELECT COUNT(DISTINCT(year(Addtime))) yearname FROM `duwebstat`; -- 按月汇总,统计: select sum(mymoney) as totalmoney, count(*) as sheets from web_product group by date_format(p_createtime, '%Y-%m'); select date_format(p_createtime, '%Y-%m') as 'month',count(*) as sheets from web_product group by date_format(p_createtime, '%Y-%m'); select DATE_FORMAT(p_createtime,'%Y%m') months,count(*) as sheets from web_product where 1=1 GROUP BY months ORDER BY months desc; SELECT DATE_FORMAT(p_createtime,'%Y%m') months,COUNT(*) COUNT FROM web_product GROUP BY months; SELECT year(p_createtime) as 'yearname',month(`p_createtime`) as 'monthname',count(*) as'sheet' FROM `web_product` group by yearname,monthname; SELECT year(Addtime) as 'yearname',month(`Addtime`) as 'monthname',count(*) as'sheet' FROM `duwebstat` group by yearname,monthname; SELECT count(DISTINCT(concat(cast(year(Addtime) as char(50)),cast(month(Addtime) as char(50))))) FROM duwebstat; select DATE_FORMAT(Addtime,'%Y-%m') months,count(*) as sheets from duwebstat where 1=1 GROUP BY months ORDER BY months desc; -- 按季度汇总,统计: select sum(mymoney) as totalmoney,count(*) as sheets from web_product group by concat(date_format(p_createtime, '%Y'),FLOOR((date_format(p_createtime, '%m')+2)/3)); select count(*) as sheets from web_product group by concat(date_format(p_createtime, '%Y'),FLOOR((date_format(p_createtime, '%m')+2)/3)); select concat(date_format(p_createtime,'%Y'),FLOOR((date_format(p_createtime, '%m')+2)/3)) quarters,sum(duration) dur from web_product where 1=1 GROUP BY quarters ORDER BY quarters desc; select concat(date_format(p_createtime,'%Y'),FLOOR((date_format(p_createtime, '%m')+2)/3)) quarters,count(*) as sheets from web_product where 1=1 GROUP BY quarters ORDER BY quarters desc; SELECT id, year(p_createtime),quarter(`p_createtime`) FROM `web_product`; SELECT year(p_createtime) as 'yearname',quarter(`p_createtime`) as 'quartername',count(*) as'sheet' FROM `web_product` group by yearname,quartername; SELECT DISTINCT(concat(cast(year(p_createtime) as char(50)),cast(quarter(p_createtime) as char(50)))) FROM web_product; SELECT count(DISTINCT(concat(cast(year(p_createtime) as char(50)),cast(quarter(p_createtime) as char(50))))) FROM web_product; select CAST(122 as CHAR); select now(); select quarter(now()); SELECT CAST(123 AS CHAR); select concat(DATE_FORMAT(now(),'%Y'),cast(quarter(now()) as char(20))); SELECT year(Addtime) as 'yearname',quarter(`Addtime`) as 'quartername',count(*) as'sheet' FROM `duwebstat` group by yearname,quartername; SELECT COUNT(DISTINCT(year(Addtime))) yearname FROM `duwebstat`; SELECT count(DISTINCT(concat(cast(year(Addtime) as char(50)),cast(quarter(Addtime) as char(50))))) FROM duwebstat; -- 按周统计
select DATE_FORMAT(p_createtime,'%Y%u') weeks,count(*) as sheets from web_product where 1=1 GROUP BY weeks ORDER BY weeks desc; select DATE_FORMAT(p_createtime,'%Y-%u') weeks,count(*) as sheets from web_product where 1=1 GROUP BY weeks ORDER BY weeks desc; SELECT DATE_FORMAT(p_createtime,'%Y%u') weeks,COUNT(*) COUNT FROM web_product GROUP BY weeks; SELECT DATE_FORMAT(Addtime,'%Y-%u') weeks,COUNT(*) COUNT FROM duwebstat GROUP BY weeks; select DISTINCT(DATE_FORMAT(p_createtime,'%Y-%u')) from web_product; SELECT year(p_createtime) yearname,week(p_createtime) weeks,COUNT(*) COUNT FROM web_product GROUP BY weeks,yearname; SELECT year(Addtime) yearname,week(Addtime) weeks,COUNT(*) COUNT FROM duwebstat GROUP BY weeks,yearname; select DATE_FORMAT(Addtime,'%Y%u') weeks,count(*) as sheets from duwebstat where 1=1 GROUP BY weeks ORDER BY weeks desc; select count(DISTINCT(DATE_FORMAT(Addtime,'%Y-%u'))) from duwebstat; -- 按日统计
-- https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format SELECT DATE_FORMAT(p_createtime,'%Y%m%d') days,COUNT(*) COUNT FROM web_product GROUP BY days; SELECT DATE_FORMAT(p_createtime,'%Y-%m-%d') days,COUNT(*) COUNT FROM web_product GROUP BY days; SELECT DATE_FORMAT(Addtime,'%Y-%m-%d') days,COUNT(*) as sheet FROM duwebstat GROUP BY days; select count(DISTINCT(DATE_FORMAT(Addtime,'%Y-%m-%d'))) from duwebstat; --
SELECT DATE_FORMAT('2009-10-04 22:23:00', '%W %M %Y'); --
-- 按小时:Hour select date_format(p_createtime, '%Y-%m-%d %H'),count(*) as sheets from web_product group by date_format(p_createtime, '%Y-%m-%d %H'); select date_format(p_createtime, '%Y-%m-%d %H'),count(*) as sheets from web_product group by date_format(p_createtime, '%Y-%m-%d %H') limit 0,30; select date_format(Addtime, '%Y-%m-%d %H') as hours,count(*) as sheet from duwebstat group by date_format(Addtime, '%Y-%m-%d %H'); select sum(mymoney) as totalmoney,count(*) as sheets from web_product group by date_format(p_createtime, '%Y-%m-%d %H '); -- 查询 本年度的数据: SELECT * FROM web_product WHERE year(FROM_UNIXTIME(p_createtime)) = year(curdate()); -- 查询数据附带季度数: SELECT id, quarter(FROM_UNIXTIME(p_createtime)) FROM web_product; -- 查询 本季度的数据: SELECT * FROM web_product WHERE quarter(FROM_UNIXTIME(p_createtime)) = quarter(curdate()); -- 本月统计: select * from web_product where month(p_createtime) = month(curdate()) and year(p_createtime) = year(curdate()); -- 本周统计: select * from web_product where month(p_createtime) = month(curdate()) and week(p_createtime) = week(curdate());
MySql: Year, Quarter, Month, Day, Hour statistics的更多相关文章
- MySQL/MariaDB数据库的mysqldump工具备份还原实战
MySQL/MariaDB数据库的mysqldump工具备份还原实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.mysqldump概述 1>.逻辑备份工具 mysq ...
- mysql select日期格式
mysql表中datatime类型存储为2016-01-10,C#直接select 后,在datatable里面看,变成01/10/2016,需要还原回去,使用select DATE_FORMAT(列 ...
- mysql函数大全
对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的最左面字符的ASCII代码值.如果str是空字符串,返回0.如果str是NULL,返回NULL. mysql& ...
- mysql内置函数大全
mysql 字符串函数用法集合 ASCII(str)返回字符串str的最左面字符的ASCII代码值. mysql> select ascii('d'); +------------+ | asc ...
- mysql创建定时任务
一.前言 自 MySQL5.1.6起,增加了一个非常有特色的功能–事件调度器(Event Scheduler),可以用做定时执行某些特定任务(例如:删除记录.对数据进行汇总等等),来取代原先只能由操作 ...
- mysql 定时任务
mysql 5.1以上支持定时任务. SHOW VARIABLES LIKE 'event_scheduler'; 检查是否已开启该功能 开启计划任务功能: SET GLOBAL event_sc ...
- mysql时间格式化,按时间段查询的MySQL语句
描述:有一个会员表,有个birthday字段,值为'YYYY-MM-DD'格式,现在要查询一个时间段内过生日的会员,比如'06-03'到'07-08'这个时间段内所有过生日的会员. SQL语句: Se ...
- MySQL日期时间函数大全 转
DAYOFWEEK(date) 返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)mysql> select DAYOFWEEK('1998-02-03'); ...
- Mysql 事件(定时任务)
mysql 创建任务(事件) 1.检查数据库事件是否开启,如果 event_scheduler 等于 NO表示开启 SELECT @@event_scheduler; SHOW VARIABLES L ...
随机推荐
- 跟繁琐的命令行说拜拜!Gerapy分布式爬虫管理框架来袭!
背景 用 Python 做过爬虫的小伙伴可能接触过 Scrapy,GitHub:https://github.com/scrapy/scrapy.Scrapy 的确是一个非常强大的爬虫框架,爬取效率高 ...
- PHP删除当前目录及其目录下的所有文件
使用PHP遍历一个目录下的所有目录及文件,并删除该目录及其目录下的所有子目录和文件,本次代码通过递归的方式来实现. 用到的函数: scandir($path) 遍历一个目录下所有文件并返回数组. un ...
- MyBatis 多表关联查询
多表关联查询 一对多 单条SQL实现. //根据部门编号查询出部门和部门成员姓名public dept selectAll() thorws Excatipon; //接口的抽象方法 下面是对应接口的 ...
- Python档案袋(脚本执行和输入输出)
脚本的执行: 1.执行时确定解释器为Python3 python3 脚本名 2.在代码开始行确定解释器为Python3,与shell相似 env命令表示全局搜索Python3解释器 #!/usr/bi ...
- Jquery百宝箱
引入jquery <script src="https://blog-static.cnblogs.com/files/dongxiaodong/jquery-3.3.1.min.js ...
- Spring Boot 2.1.0 已发布,7 个重大更新!
距离<重磅:Spring Boot 2.0 正式发布!>已经过去大半年了,而 Spring Boot 2.1.0 在 10 月底就发布了,我们来看下 Spring Boot 2.1.0 都 ...
- mysql 开发基础系列3 日期数据类型
日期类型 如果要用来表示年月日,通常用DATE 来表示. 如果要用来表示年月日时分秒,通常用DATETIME 表示. 如果只用来表示时分秒,通常用TIME 来表示. TIMESTAMP表示格式 :YY ...
- Java 容器 & 泛型:六、容器讲到为什么要使用泛型
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket ArrayList是集合类中无处不在的,泛型也是,泛型对集合类尤其有用.但是为啥要使用泛型?理解好了这 ...
- Netty实现简单HTTP代理服务器
自上次使用Openresty+Lua+Nginx的来加速自己的网站,用上了比较时髦的技术,感觉算是让自己的网站响应速度达到极限了,直到看到了Netty,公司就是打算用Netty来替代Openresty ...
- [转&精]IO_STACK_LOCATION与IRP的一点笔记
IO_STACK_LOCATION和IRP算是驱动中两个很基础的东西,为了理解这两个东西,找了一点资料. 1. IRP可以看成是Win32窗口程序中的消息(Message),DEVICE_OBJECT ...