mysql查询上一天/上一小时的数据

查看当前日期:

select current_date()
返回结果:2015-01-26

select now()
返回结果:2015-01-26 11:57:52

DATE_SUB() 函数从日期减去指定的时间间隔。

DATE_SUB(date,INTERVAL expr type)
具体可参考:http://www.w3school.com.cn/sql/func_date_sub.asp

FROM_UNIXTIME():unix时间戳格式化为日期函数

UNIX_TIMESTAMP() :日期转时间戳

DATE_FORMAT(date,format):格式化日期函数

故 :

查询上一天数据 sql:

select * from 数据表 where cdate between date_sub(date_format(current_date(),'%Y-%m-%d 00:00:00'),interval 1 day) and date_format(current_date(),'%Y-%m-%d 00:00:00')
查询上一小时数据 sql:

select * from 数据表 where cdate between date_sub(date_format(now(),'%Y-%m-%d %H:00:00'),interval 1 hour) and date_format(now(),'%Y-%m-%d %H:00:00')




你可能感兴趣的:(数据库笔记)