根据时间段统计每天的数据
SELECT
dayTb.cday '日期',
IFNULL(tNumTb.num,0) '人数'
FROM (
SELECT @cdate := DATE_ADD(@cdate, INTERVAL +1 DAY) cday
FROM( SELECT @cdate := DATE_ADD('2021-05-10', INTERVAL -1 DAY) FROM t_customer) t0
WHERE date(@cdate) <= DATE_ADD('2021-05-30', INTERVAL -1 DAY)
) dayTb
LEFT JOIN(
SELECT date(t.CREATE_DATE) cday,count(t.CUSTOMER_ID) num FROM t_customer t
WHERE t.CREATE_DATE >='2021-03-10'
GROUP BY cday
) tNumTb ON tNumTb.cday = dayTb.cday
根据年份统计每月数据
select
sum(case month(CREATE_DATE) when '1' then 1 else 0 end) as Jan,
sum(case month(CREATE_DATE) when '2' then 1 else 0 end) as Feb,
sum(case month(CREATE_DATE) when '3' then 1 else 0 end) as Mar,
sum(case month(CREATE_DATE) when '4' then 1 else 0 end) as Apr,
sum(case month(CREATE_DATE) when '5' then 1 else 0 end) as May,
sum(case month(CREATE_DATE) when '6' then 1 else 0 end) as June,
sum(case month(CREATE_DATE) when '7' then 1 else 0 end) as July,
sum(case month(CREATE_DATE) when '8' then 1 else 0 end) as Aug,
sum(case month(CREATE_DATE) when '9' then 1 else 0 end) as Sept,
sum(case month(CREATE_DATE) when '10' then 1 else 0 end) as Oct,
sum(case month(CREATE_DATE) when '11' then 1 else 0 end) as Nov,
sum(case month(CREATE_DATE) when '12' then 1 else 0 end) as Dece
from t_customer
where year(CREATE_DATE)= '2021';
今天的数据
select * from 表名 where to_days(时间字段名) = to_days(now());
昨天
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1
近7天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)
近30天
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)
本月
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
上月
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1
查询本季度数据
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1
查询上季度数据
select * from 表名 where QUARTER(时间字段)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
查询距离当前现在6个月的数据
select * from 表名 where 时间字段 between date_sub(now(),interval 6 month) and now();
查询某一天中每个小时的数量, 如果某个时间段没有数据, 就填充 0.
select a.order_hour as order_hour, ifnull(b.num, 0) as num
from (
SELECT 0 AS order_hour UNION ALL SELECT 1 AS order_hour UNION ALL
SELECT 2 AS order_hour UNION ALL SELECT 3 AS order_hour UNION ALL
SELECT 4 AS order_hour UNION ALL SELECT 5 AS order_hour UNION ALL
SELECT 6 AS order_hour UNION ALL SELECT 7 AS order_hour UNION ALL
SELECT 8 AS order_hour UNION ALL SELECT 9 AS order_hour UNION ALL
SELECT 10 AS order_hour UNION ALL SELECT 11 AS order_hour UNION ALL
SELECT 12 AS order_hour UNION ALL SELECT 13 AS order_hour UNION ALL
SELECT 14 AS order_hour UNION ALL SELECT 15 AS order_hour UNION ALL
SELECT 16 AS order_hour UNION ALL SELECT 17 AS order_hour UNION ALL
SELECT 18 AS order_hour UNION ALL SELECT 19 AS order_hour UNION ALL
SELECT 20 AS order_hour UNION ALL SELECT 21 AS order_hour UNION ALL
SELECT 22 AS order_hour UNION ALL SELECT 23 AS order_hour
) as a
LEFT JOIN
(
SELECT HOUR(CREATED_TIME) AS order_hour, count(ID) AS num FROM dd_dealtask_record
WHERE CREATED_TIME >= str_to_date('2023-02-17 00:00:00','%Y-%m-%d %T')
AND CREATED_TIME <= str_to_date('2023-02-17 23:59:59','%Y-%m-%d %T')
GROUP BY order_hour
) b ON a.order_hour=b.order_hour
ORDER BY order_hour;