我想在过去7天内提取记录。这是我一直试图开始工作的选择声明:
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
我尝试了各种各样的where子句,如
where time between curdate() and curdate() - INTERVAL 7 DAY
和
where from_unixtime(time,'%yyyy-%mm-%dd') between curdate() and curdate() - INTERVAL 7 DAY
和
where date(time) between curdate() and curdate() - INTERVAL 7 DAY
选择curdate() - 导致以2012-11-08格式显示的日期
答案 0 :(得分:2)
你的最后一个化身几乎就在那里。但是,您需要比较苹果和苹果。由于time
是整数,因此您需要将其转换为MySQL date/time functions才能使用。
WHERE DATE(FROM_UNIXTIME(time)) between CURDATE() and CURDATE() - INTERVAL 7 DAY
根据您的用例,您实际上只需要FROM_UNIXTIME():
WHERE FROM_UNIXTIME(time) between CURDATE() and CURDATE() - INTERVAL 7 DAY