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;