MySQL关于根据日期查询数据的sql语句

查询往前7天的数据:

 

[html] view plain copy
 
 print?
  1. select * from 数据表  where  DATE_SUB(CURDATE(), INTERVAL 7 DAY) <=  你要判断的时间字段名  
[html] view plain copy
 
 print?
  1.   

 

 

查询往前30天的数据:

[html] view plain copy
 
 print?
  1. select * from 数据表  where  DATE_SUB(CURDATE(), INTERVAL 30 DAY) <=  你要判断的时间字段名  
[html] view plain copy
 
 print?
  1.   

 

查询在某段日期之间的数据:

[html] view plain copy
 
 print?
  1. select * from 数据表  where 时间字段名 BETWEEN '2016-02-01' AND '2016-02-05'  

 

查询往前3个月的数据:

[html] view plain copy
 
 print?
  1. select * from 数据表  where 时间字段名 BETWEEN DATE_SUB(NOW(),INTERVAL 3 MONTH) AND NOW()  
[html] view plain copy
 
 print?
  1.   

 

查询往前一年的数据:

[html] view plain copy
 
 print?
  1. select * from 数据表  where 时间字段名 BETWEEN DATE_SUB(NOW(),INTERVAL 1 YEAR) AND NOW()  
[html] view plain copy
 
 print?
  1.   

 

查询本月的数据

 

[html] view plain copy
 
 print?
  1. select * from 数据表 where DATE_FORMAT(时间字段名,'%Y-%m')=DATE_FORMAT(NOW(),'%Y-%m')  

 

 

查询上月的数据

select * from 数据表 where DATE_FORMAT(时间字段名,'%Y-%m')=DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 1 MONTH),'%Y-%m')


查询本周的数据

 

[html] view plain copy
 
 print?
  1. select * from 数据表 where YEARWEEK(DATE_FORMAT(时间字段名,'%Y-%m-%d')) = YEARWEEK(NOW())  

 

 

查询上周数据

 

[html] view plain copy
 
 print?
  1. select * from 数据表 where YEARWEEK(DATE_FORMAT(CREATE_TIME,'%Y-%m-%d')) = YEARWEEK(NOW())-1  



 

 

希望大家发现博客有错误和我提出,博主会第一时间更新文章.

谢谢.

posted @ 2017-10-16 09:32  朝阳的向日葵  阅读(25094)  评论(0编辑  收藏  举报