Counting rows where a timestamp is less than 24 hours old

前端 未结 4 1363
野的像风
野的像风 2021-01-03 19:28

For the query below, how could I count the number of rows where datesent is less than 24 hours old? (The field datesent is a timestamp).

T

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-03 20:15

    I see this is a old one though for anybody that gets here trough an online search later on, I would like to clarify that the question is about a timestamp, not datetime. I needed a count based on timestamp. Easy fix is to convert the datetime to unix_timestamp. Tested on MySQL 5.7

    SELECT COUNT(*) AS cnt
    FROM PRIVATEMESSAGE pm
    WHERE pm.datesent >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))
    

提交回复
热议问题