How to select records from last 24 hours using SQL?

后端 未结 11 2131
Happy的楠姐
Happy的楠姐 2020-12-04 04:41

I am looking for a where clause that can be used to retrieve records for the last 24 hours?

相关标签:
11条回答
  • 2020-12-04 05:26

    In Oracle (For last 24 hours):

    SELECT  *
    FROM    my_table
    WHERE   date_column >= SYSDATE - 24/24
    

    In case, for any reason, you have rows with future dates, you can use between, like this:

    SELECT  *
    FROM    my_table
    WHERE   date_column BETWEEN (SYSDATE - 24/24) AND SYSDATE
    
    0 讨论(0)
  • 2020-12-04 05:27

    in postgres, assuming your field type is a timestamp:

    select * from table where date_field > (now() - interval '24 hour');

    0 讨论(0)
  • 2020-12-04 05:30

    Hello i now it past a lot of time from the original post but i got a similar problem and i want to share.

    I got a datetime field with this format YYYY-MM-DD hh:mm:ss, and i want to access a whole day, so here is my solution.

    The function DATE(), in MySQL: Extract the date part of a date or datetime expression.

    SELECT * FROM `your_table` WHERE DATE(`your_datatime_field`)='2017-10-09'
    

    with this i get all the row register in this day.

    I hope its help anyone.

    0 讨论(0)
  • 2020-12-04 05:34

    In MySQL:

    SELECT  *
    FROM    mytable
    WHERE   record_date >= NOW() - INTERVAL 1 DAY
    

    In SQL Server:

    SELECT  *
    FROM    mytable
    WHERE   record_date >= DATEADD(day, -1, GETDATE())
    

    In Oracle:

    SELECT  *
    FROM    mytable
    WHERE   record_date >= SYSDATE - 1
    

    In PostgreSQL:

    SELECT  *
    FROM    mytable
    WHERE   record_date >= NOW() - '1 day'::INTERVAL
    

    In Redshift:

    SELECT  *
    FROM    mytable
    WHERE   record_date >= GETDATE() - '1 day'::INTERVAL
    

    In SQLite:

    SELECT  *
    FROM    mytable
    WHERE   record_date >= datetime('now','-1 day')
    

    In MS Access:

    SELECT  *
    FROM    mytable
    WHERE   record_date >= (Now - 1)
    
    0 讨论(0)
  • 2020-12-04 05:38

    Which SQL was not specified, SQL 2005 / 2008

    SELECT yourfields from yourTable WHERE yourfieldWithDate > dateadd(dd,-1,getdate())
    

    If you are on the 2008 increased accuracy date types, then use the new sysdatetime() function instead, equally if using UTC times internally swap to the UTC calls.

    0 讨论(0)
提交回复
热议问题