问题
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