--  统计

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. Eclipse 中 Spring 项目的 XML 配置文件报错 Referenced file contains errors

    原来运行正常的项目,突然在applicationContext.xml 文件头报错 总结一下网上的解决方案: 1.有可能网络状况不好导致 如果使用Maven构建项目,spring在加载xsd文件时总是 ...

  2. Kali学习笔记6:二层发现

    先介绍下ARPING命令: arping命令是用于发送ARP请求到一个相邻主机的工具 arping使用arp数据包,通过PING命令检查设备上的硬件地址.能够测试一个IP地址是否是在网络上已经被使用, ...

  3. 【app】Appium-desktop界面介绍

    在appium主界面的host输入127.0.0.1 然后点击Start Server即可开启appium server 我们来说说advanced选项 Server Address: 为appium ...

  4. 关于I/O编程

    IO在计算机中指Input/Output,也就是输入和输出 由于程序在运行时,数据是驻留在内存中的,并由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口 IO编 ...

  5. 剑指offer【05】- 用两个栈实现队列(java)

    题目:用两个栈实现队列 考点:栈和队列 题目描述:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解题思路:每次psuh是时先将stack2清空放入stck1(保 ...

  6. python之使用位运算符实现加法运算

    一哥们去笔试,回来后跟我说了一通面试题,其中有一道题让我很感兴趣: 不使用+号实现加法运算 刚听到后,一脸懵逼,不使用+号怎么算? 问了朋友他也没做这题,不过仔细想了下,不使用+号,是否可以使用其他运 ...

  7. Kubernetes 服务入口管理与 Nginx Ingress Controller

    Kubernetes 具有强大的副本,动态扩容等特性,每一次 Pod 的变化 IP 地址都会发生变化,所以 Kubernetes 引进了 Service 的概念.Kubernetes 中使用 Serv ...

  8. 深入学习主成分分析(PCA)算法原理(Python实现)

    一:引入问题 首先看一个表格,下表是某些学生的语文,数学,物理,化学成绩统计: 首先,假设这些科目成绩不相关,也就是说某一科目考多少分与其他科目没有关系,那么如何判断三个学生的优秀程度呢?首先我们一眼 ...

  9. 手把手在Ubuntu上面安装Docker

    一.环境准备 1.Ubuntu64位系统(目前docker仅支持64位系统) 2.官方支持的Ubuntu版本(1)Ubuntu Trusty 14.04(LTS)(2)Ubuntu Precise 1 ...

  10. Jenkins结合.net平台工具之Nunit

    有时候我们需要对从git上拉取的项目进行单元测通过以后才可以发布到测试环境,.net平台下单元测试的框架也很多例如mstest,nunit,xunit等,下面以Nunit为例讲解如何通过Jenkins ...