select*from [table] where to_days([time_field]) = to_days(now())
昨天
1
select*from [table] where to_days(now())-to_days([time_field]) <=1
N天
1
select*from [table] where to_days(now())-to_days([time_field]) <= N
近7天
1
select*from [table] where date_sub(curdate(),INTERVAL7DAY) <=date([time_field])
近30天
1
select*from [table] where date_sub(curdate(),INTERVAL30DAY) <=date([time_field])
本月
1
select*from [table] where date_format([time_field], '%Y%m') = date_format(curdate() , '%Y%m')
上月
1
select*from [table] where period_diff(date_format(now(), '%Y%m'),date_format([time_field], '%Y%m')) =1
更细粒度的查询方式使用str_to_date
1
select*from [table] where str_to_date([time_field],'%Y-%m-%d %H:%i:%s')>='2012-06-28 08:00:00'and str_to_date([time_field],'%Y-%m-%d %H:%i:%s')<='2012-06-28 09:59:59'
按年汇总,统计:
1
selectsum(mymoney) as totalmoney, count(*) as sheets from [table] groupby date_format([time_field], '%Y')
按月汇总,统计:
1
selectsum(mymoney) as totalmoney, count(*) as sheets from [table] groupby date_format([time_field], '%Y-%m')
按季度汇总,统计:
1
selectsum(mymoney) as totalmoney,count(*) as sheets from [table] groupby concat(date_format([time_field], '%Y'),FLOOR((date_format([time_field], '%m')+2)/3))
按小时:
1
selectsum(mymoney) as totalmoney,count(*) as sheets from [table] groupby date_format([time_field], '%Y-%m-%d %H ')