how to use curdate() in where clause against unixtimestamp (bigint) column

ぐ巨炮叔叔 提交于 2019-12-11 05:37:18

问题


I am trying to pull records in the past 7 days. This is my select statement that I have been trying to get to work:

select from_unixtime(time,'%m/%d/%y') as fdate, from_unixtime(time,'%h:%m:%s') as ftime
from mdl_log
where from_unixtime(time,'%y-%m-%d')  between curdate() and curdate() - INTERVAL 7 DAY

I have tried various incarnations of the where clause like

where time between curdate() and curdate() - INTERVAL 7 DAY

and

where from_unixtime(time,'%yyyy-%mm-%dd')  between curdate() and curdate() - INTERVAL 7 DAY

and

where date(time) between curdate() and curdate() - INTERVAL 7 DAY

select curdate() - results in the date showing in this format 2012-11-08


回答1:


You were almost there with your last incarnation. However, you need to compare apples to apples. Since time is an integer, you need to convert it for MySQL date/time functions to use.

WHERE DATE(FROM_UNIXTIME(time)) between CURDATE() and CURDATE() - INTERVAL 7 DAY

Given your use case, you really only need FROM_UNIXTIME():

WHERE FROM_UNIXTIME(time) between CURDATE() and CURDATE() - INTERVAL 7 DAY


来源:https://stackoverflow.com/questions/13293570/how-to-use-curdate-in-where-clause-against-unixtimestamp-bigint-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!