Mysql Query back to midnight only

11,101

Solution 1

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'

Solution 2

SELECT * FROM ip_stats WHERE date >= ( NOW() - INTERVAL 20 MINUTE ) AND date >= CURDATE() and ip='$ip'
Share:
11,101
Admin
Author by

Admin

Updated on August 05, 2022

Comments

  • Admin
    Admin over 1 year

    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.

    Any help would be appreciated.

  • Jason
    Jason about 12 years
    you're going to want a >=, not a >
  • Admin
    Admin about 12 years
    that worked perfect thank you, i don't know why i didn't think of that.
  • The Onin
    The Onin over 7 years
    I think this is the perfect solution for getting records from the current day.