mysql时间相关数据统计

MySql按周,按月,按日分组统计数据

<!-- 按日查询 -->  
SELECT DATE_FORMAT(created_date,'%Y-%m-%d') as time,sum(money) money FROM o_finance_detail where org_id = 1000  GROUP BY  time  
<!-- 按月查询 -->  
SELECT DATE_FORMAT(created_date,'%Y-%m') as time,sum(money)  money FROM o_finance_detail where org_id = 1000  GROUP BY  time  
<!-- 按年查询 -->  
SELECT DATE_FORMAT(created_date,'%Y') as time,sum(money)  money FROM o_finance_detail where org_id = 1000  GROUP BY  time   
<!-- 按周查询 -->  
SELECT DATE_FORMAT(created_date,'%Y-%u') as time,sum(money)  money FROM o_finance_detail where org_id = 1000  GROUP BY  time  
DATE_FORMAT(date,format) 
根据format字符串格式化date值。下列修饰符可以被用在format字符串中: 
%M 月名字(January……December) 
%W 星期名字(Sunday……Saturday) 
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。) 
%Y 年, 数字, 4%y 年, 数字, 2%a 缩写的星期名字(Sun……Sat) 
%d 月份中的天数, 数字(00……31) 
%e 月份中的天数, 数字(0……31) 
%m 月, 数字(01……12) 
%c 月, 数字(1……12) 
%b 缩写的月份名字(Jan……Dec) 
%j 一年中的天数(001……366) 
%H 小时(00……23) 
%k 小时(0……23) 
%h 小时(01……12) 
%I 小时(01……12) 
%l 小时(1……12) 
%i 分钟, 数字(00……59) 
%r 时间,12 小时(hh:mm:ss [AP]M) 
%T 时间,24 小时(hh:mm:ss) 
%S 秒(00……59) 
%s 秒(00……59) 
%p AM或PM 
%w 一个星期中的天数(0=Sunday ……6=Saturday ) 
%U 星期(0……52), 这里星期天是星期的第一天 
%u 星期(0……52), 这里星期一是星期的第一天 
%% 一个文字“%”。

按时间分组 统计 table的rowspan跨度

$cjrili_group = Db::query("SELECT time,count(*)  row_num FROM think_cjrili where 1  GROUP BY  time");

统计最近时间以及某时间区间的数据

-- 昨天的
select * from `order` where package_id = 1 and order_status = 1 and TO_DAYS(NOW())-TO_DAYS(pay_time)=1;
-- 昨天到现在的
select * from `order` where sid = 1 and order_status = 1 and TO_DAYS(NOW())-TO_DAYS(pay_time)<=1;
-- 今天的
select * from `order` where sid = 1 and order_status = 1 and TO_DAYS(NOW())-TO_DAYS(pay_time)<1;
-- 七天(一周)
select * from `order` where sid = 1 and order_status = 1 and date_sub(CURDATE(),INTERVAL 7  DAY) <= DATE(pay_time);
-- 30天(一個月)
SELECT * FROM `order` where sid = 1 and order_status = 1 and DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date(pay_time);
-- 某区间的
select * FROM `order` where pay_time between  '2019-07-24'  and '2019-07-30';

-- 查询当月的
SELECT * FROM score WHERE date_format(time,'%Y-%m') = date_format(now(),'%Y-%m')
-- score是表名。 time是要判断的时间的字段名

-- 具体查询某一个月的
SELECT * FROM score WHERE date_format(time,'%Y-%m') = '2019-12'



#距离当前六个月(没有数据的不显示)
SELECT DATE_FORMAT(found_time,'%Y-%m') as time,count(0) FROM lc_registration_form WHERE status = 1 and found_time BETWEEN DATE_SUB(NOW(), INTERVAL 6 MONTH) AND NOW() GROUP BY time ORDER BY time;


#距离当前六个月(没有数据的补0)
SELECT
    CONVERT (t2.year_month_str, CHAR) `month`,
    IFNULL(count(i.id), 0) AS number  
FROM
    (
        SELECT
            @rownum :=@rownum + 1 AS num,
            date_format(
                DATE_SUB(now(), INTERVAL @rownum MONTH),
                '%Y-%m'
            ) AS year_month_str
        FROM
            (SELECT @rownum := - 1) AS r_init,
            (
                SELECT
                    c.id
                FROM
                    lc_registration_form c
                LIMIT 6
            ) AS c_init
    ) t2
LEFT JOIN lc_registration_form AS i ON (
    CONCAT(
        DATE_FORMAT(i.found_time, '%Y'),
        '-',
        DATE_FORMAT(i.found_time, '%m')
    ) = t2.year_month_str
    AND i.`status` = 1
)
GROUP BY
    t2.year_month_str
ORDER BY 
		t2.year_month_str;


#按年统计所有数据(没有数据的不显示)
SELECT
    YEAR(approval_time) approvalTime,
		count(id) countNum
FROM
    lc_registration_form
		WHERE `status` = 2
GROUP BY
    YEAR(approval_time)
ORDER BY
    YEAR(approval_time) DESC;
		
		
#统计近几年的数据(剪掉数字是几就是几年)(没有数据的不显示)
select
YEAR(approval_time),
count(0) num
from lc_registration_form where YEAR(approval_time)>year(now())-5 GROUP BY YEAR(approval_time) ORDER BY YEAR(approval_time) DESC;
		
		
		
#统计近几年的数据没有的就补0
SELECT
    CONVERT (t2.year_str, CHAR) `year`,
    IFNULL(count(i.id), 0) AS number  
FROM
    (
        SELECT
            @rownum :=@rownum + 1 AS num,
            date_format(
                DATE_SUB(now(), INTERVAL @rownum YEAR),
                '%Y'
            ) AS year_str
        FROM
            (SELECT @rownum := - 1) AS r_init,
            (
                SELECT
                    c.id
                FROM
                    lc_registration_form c
                LIMIT 6
            ) AS c_init
    ) t2
LEFT JOIN lc_registration_form AS i ON (
    CONCAT(
        DATE_FORMAT(i.approval_time, '%Y')
    ) = t2.year_str
		AND i.`status` = 2
)
GROUP BY
    t2.year_str
ORDER BY 
		t2.year_str;




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值