mysql查询今天、昨天、本周、上周、本月、上月数据

查询今天数据:

select * from tj where to_days(now()) = to_days(tjsj) 

查询昨天数据:

select * from tj where to_days(now()) - to_days(tjsj) = 1

查询本周数据:

select * from tj where tjsj >= subdate(curdate(),date_format(curdate(),'%w')-1) and tjsj <= subdate(curdate(),date_format(curdate(),'%w')-7)

查询上周数据:

select * from tj where tjsj >= subdate(curdate(),date_format(curdate(),'%w')+6) and tjsj <= subdate(curdate(),date_format(curdate(),'%w')-0)

查询本月数据:

select * from tj where date_format(tjsj,'%y-%m')=date_format(curdate(),'%y-%m')

查询上月数据:

select * from tj where date_format(tjsj,'%y-%m')=date_format(date_sub(curdate(), interval 1 month),'%y-%m')

你可能感兴趣的:(Mysql)