MySQL 按時間統計數據

1.統計當天數據量

select
	count(*)
from 
   表名
where to_days(時間字段名) = to_days(now())

2.統計昨天數據量

SELECT
	count(*)
FROM 
  表名
WHERE to_days(時間字段名) = to_days(now())-1

3.統計n天前的數據量

SELECT
	count(*)
FROM 
  表名
WHERE to_days(時間字段名) = to_days(now())-n

4.統計本週的數據量

select count(*) 
   from 表名
where 
   YEARWEEK(date_format(時間字段名,'%Y-%m-%d')) = YEARWEEK(now());

5.統計上週數據量

select count(*) 
   from 表名
where 
   YEARWEEK(date_format(時間字段名,'%Y-%m-%d')) = YEARWEEK(now())-1;

6.統計本月的數據

select count(*)  
  where 
date_format(時間字段名,'%Y-%m')=date_format(now(),'%Y-%m')

7.統計上個月的數據

SELECT  
  count(*) 
FROM 表名 
  WHERE 
YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;

8.統計近30天的數據

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(時間字段名)

9.統計今年數據

SELECT count(*) FROM sp_content WHERE YEAR(crawled_time) = YEAR(NOW());

10.統計去年數據

SELECT count(*) FROM sp_content WHERE YEAR(crawled_time) = YEAR(NOW())-1;
相關文章
相關標籤/搜索