How to select records from last 24 hours using SQL?

后端 未结 11 2130
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:12

    In SQL Server (For last 24 hours):

    SELECT  *
    FROM    mytable
    WHERE   order_date > DateAdd(DAY, -1, GETDATE()) and order_date<=GETDATE()
    
    0 讨论(0)
  • 2020-12-04 05:13
    SELECT * 
    FROM tableName 
    WHERE datecolumn >= dateadd(hour,-24,getdate())
    
    0 讨论(0)
  • 2020-12-04 05:15
    select ...
    from ...
    where YourDateColumn >= getdate()-1
    
    0 讨论(0)
  • 2020-12-04 05:22

    MySQL :

    SELECT * 
    FROM table_name
    WHERE table_name.the_date > DATE_SUB(NOW(), INTERVAL 24 HOUR)
    

    The INTERVAL can be in YEAR, MONTH, DAY, HOUR, MINUTE, SECOND

    For example, In the last 10 minutes

    SELECT * 
    FROM table_name
    WHERE table_name.the_date > DATE_SUB(NOW(), INTERVAL 10 MINUTE)
    
    0 讨论(0)
  • 2020-12-04 05:22

    If the timestamp considered is a UNIX timestamp You need to first convert UNIX timestamp (e.g 1462567865) to mysql timestamp or data

    SELECT * FROM `orders` WHERE FROM_UNIXTIME(order_ts) > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
    
    0 讨论(0)
  • 2020-12-04 05:26
    SELECT * 
    FROM table_name
    WHERE table_name.the_date > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
    
    0 讨论(0)
提交回复
热议问题