--  统计

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的更多相关文章

  1. MySQL/MariaDB数据库的mysqldump工具备份还原实战

    MySQL/MariaDB数据库的mysqldump工具备份还原实战 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.mysqldump概述 1>.逻辑备份工具 mysq ...

  2. mysql select日期格式

    mysql表中datatime类型存储为2016-01-10,C#直接select 后,在datatable里面看,变成01/10/2016,需要还原回去,使用select DATE_FORMAT(列 ...

  3. mysql函数大全

    对于针对字符串位置的操作,第一个位置被标记为1. ASCII(str) 返回字符串str的最左面字符的ASCII代码值.如果str是空字符串,返回0.如果str是NULL,返回NULL. mysql& ...

  4. mysql内置函数大全

    mysql 字符串函数用法集合 ASCII(str)返回字符串str的最左面字符的ASCII代码值. mysql> select ascii('d'); +------------+ | asc ...

  5. mysql创建定时任务

    一.前言 自 MySQL5.1.6起,增加了一个非常有特色的功能–事件调度器(Event Scheduler),可以用做定时执行某些特定任务(例如:删除记录.对数据进行汇总等等),来取代原先只能由操作 ...

  6. mysql 定时任务

    mysql 5.1以上支持定时任务. SHOW VARIABLES LIKE 'event_scheduler';   检查是否已开启该功能 开启计划任务功能: SET GLOBAL event_sc ...

  7. mysql时间格式化,按时间段查询的MySQL语句

    描述:有一个会员表,有个birthday字段,值为'YYYY-MM-DD'格式,现在要查询一个时间段内过生日的会员,比如'06-03'到'07-08'这个时间段内所有过生日的会员. SQL语句: Se ...

  8. MySQL日期时间函数大全 转

    DAYOFWEEK(date)  返回日期date是星期几(1=星期天,2=星期一,……7=星期六,ODBC标准)mysql> select DAYOFWEEK('1998-02-03');  ...

  9. Mysql 事件(定时任务)

    mysql 创建任务(事件) 1.检查数据库事件是否开启,如果 event_scheduler 等于 NO表示开启 SELECT @@event_scheduler; SHOW VARIABLES L ...

随机推荐

  1. C#连接mysql数据库的一个例子和获取本机IP的方法

    本例子是一个最初级直接连接mysql数据库的例子,实现了往数据库插入数据的操作: string MyConnectionMysql="Server=localhost;Datbase=xxx ...

  2. 机器学习入门02 - 深入了解 (Descending into ML)

    原文链接:https://developers.google.com/machine-learning/crash-course/descending-into-ml/ 线性回归是一种找到最适合一组点 ...

  3. Xamarin.Android 嵌入web端界面

    在程序中嵌入Web端界面. 首先在前台界面上创建一个webview <android.webkit.WebView android:layout_width="match_parent ...

  4. 怎么让Word形状里的文字上下左右居中

    怎么让Word形状里的文字上下左右居中? 第一:左右居中,用段落居中方法: 第二:上下居中,选定图形,单击鼠标右键并选择“设置形状格式”,在选项卡的“文本框”中,选择中部对齐 效果图:

  5. 微信小程序内嵌业务域名内的网页

    微信小程序在2017年11月左右开放了内嵌网页的功能,即新组件<web-view>.官方文档链接:https://mp.weixin.qq.com/debug/wxadoc/dev/com ...

  6. mysql 开发进阶篇系列 6 锁问题(事务与隔离级别介绍)

    一.概述 在数据库中,数据是属于共享资源,为了保证并发访问的一致性,有效性,产生了锁.接下来重点讨论mysql锁机制的特点,常见的锁问题,以及解决mysql锁问题的一些方法或建议. 相比其他数据库,m ...

  7. Vc数据库编程基础MySql数据库的表查询功能

    Vc数据库编程基础MySql数据库的表查询功能 一丶简介 不管是任何数据库.都会有查询功能.而且是很重要的功能.上一讲知识简单的讲解了表的查询所有. 那么这次我们需要掌握的则是. 1.使用select ...

  8. https下 http的会被阻塞 This request has been blocked; the content must be served over HTTPS.

    如何在HTTPS 网页中引入HTTP资源: Mixed Content? https://segmentfault.com/q/1010000005872734/a-1020000005874533 ...

  9. Asp.Net项目的部署到Linux中(Linux + Jexus+Nginx )

    因为老项目用的Asp.Net Web API技术开发部署到Window系统上,而新项目用的是.Net Core部署到Ubuntu系统中,所以在管理切换上有些不便.于是决定将老项目的测试服部署到Ubun ...

  10. js中对象和对象创建方法

    这一次我们来说一说在JavaScript中经常会用到的一个复杂基本类型,对象,先从对象的属性讲起,再讲对象的创建方法,基本涵盖了创建对象的各种方法,大家一起学习呀~ 一.对象 要掌握对象的使用及继承, ...