MySQL查询仅返回午夜 [英] Mysql Query back to midnight only
问题描述
我有这个查询,可以查询过去二十分钟的数据库结果,现在我知道如何在小时,天等中查询,但是是否有可能只查询当天的午夜.那么什么时候运行查询以及什么时候查询回溯至午夜?
I have this query that looks up results from a database for the last twenty minutes, now i know how to look up in hours, days, etc, but is it possible to look up only as far back as midnight of that day. so when ever the query is run and what ever time it only looks back as far as midnight?
SELECT * FROM ip_stats WHERE date >= ( NOW() - INTERVAL 20 MINUTE ) and ip='$ip'
这是我的代码,但是我可以在指定的时间内替换间隔.
This is my code but is there away in which i can replace the interval for a specific time.
任何帮助将不胜感激.
推荐答案
回溯到当前日期的午夜与查看没有日期成分的当前日期相同.因此,您可以使用DATE()
将日期时间列date
截断为仅日期部分,并将其与CURDATE()
进行比较.
Looking back to midnight of the current day is the same as looking at the current date with no time component. You can therefore use DATE()
to truncate the datetime column date
to only the date portion, and compare it to CURDATE()
.
SELECT * FROM ip_stats WHERE DATE(date) = CURDATE() and ip='$ip'
这篇关于MySQL查询仅返回午夜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!