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 ...
随机推荐
- 还在使用SimpleDateFormat?
阅读本文大概需要 3.2 分钟. 前言 日常开发中,我们经常需要使用时间相关类,想必大家对SimpleDateFormat并不陌生.主要是用它进行时间的格式化输出和解析,挺方便快捷的,但是Simple ...
- bootstrap treeview实现菜单树
本博客,介绍通过Bootstrap的treeview插件实现菜单树的功能. treeview链接:http://www.htmleaf.com/Demo/201502141380.html ORM框架 ...
- 从非标准的POST数据流中提取文件
1 接收数据流转成字符串,注意编码 byte[] recv= Request.BinaryRead(Request.TotalBytes);string sourceByte = Encoding.U ...
- java mongodb的MongoOptions生产级配置
autoConnectRetry仅仅意味着驱动程序会自动尝试重新连接到意外断开连接后在服务器(一个或多个).在生产环境中,您通常需要将此设置为true. connectionsPerHost是物理连接 ...
- postgresql数据库删除时提示回话 sessions using the database
数据库命令行或者管理工具中执行删除数据库的命令, DROP DATABASE testdb; 的时候,可能会提示: ERROR: database "testdb" is bein ...
- SVN用户切换
Eclipse的SVN插件Subclipse做得很好,在svn操作方面提供了很强大丰富的功能.但到目前为止,该插件对svn用户的概念极为淡薄,不但不能方便地切换用户,而且一旦用户的帐号.密码保存之后 ...
- Hadoop YARN架构设计要点
YARN是开源项目Hadoop的一个资源管理系统,最初设计是为了解决Hadoop中MapReduce计算框架中的资源管理问题,但是现在它已经是一个更加通用的资源管理系统,可以把MapReduce计算框 ...
- 日志切割工具logrotate解决Tomcat catalina.out日志过大的问题
一.介绍日志切割logrotate 对于Linux系统安全来说,日志文件是极其重要的工具.不知为何,我发现很多运维同学的服务器上都运行着一些诸如每天切分Nginx日志之类的CRON脚本,大家似乎遗忘了 ...
- Linux中文件MD5校验
md5sum命令用于生成文件的md5数字摘要,并可以验证文件内容是否发生了改变,间接地还可以检验两个文件内容是否完全相同.因为md5sum是读取文件内容来计算校验码的,因此只能验证文件内容,而无法验证 ...
- python列表类型
列表类型简介 列表类型是一个容器,它里面可以存放任意数量.任意类型的数据. 例如下面的几个列表中,有存储数值的.字符串的.内嵌列表的.不仅如此,还可以存储其他任意类型. >>> L ...