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

2017-12-14 13:46:14

查询往前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  



  • 2018-01-17 01:55:49

    Android设置控件不可点击

    学会安卓控件的不可点击,再加上控件的透明度,估计你会做出不错的效果来。