如何使用 unix 时间戳获取最近 24 小时的记录

How to fetch last 24 hour record using unix timestamps

我在 MySQL 中有两个 table,我想获取评论的名称和数量。我该怎么做?

这是我的 table un_users:

id      wallet_address          username    
1       xxxxxxxxxxxxxx          abc
2       xxxxxxxxxxxxxx          xyz
3       xxxxxxxxxxxxxx          def
4       xxxxxxxxxxxxxx          jkl 

这是我的 table coin_review:

id      wallet_address      review                  review_creation_time
1       xxxxxxxxxxxxx       Lorem Ipsum1            1632812650  
2       xxxxxxxxxxxxx       Lorem Ipsum2            1632812211
3       xxxxxxxxxxxxx       Lorem Ipsum3            1632812650
4       xxxxxxxxxxxxx       Lorem Ipsum4            1632814574

我试过以下代码但没有用:

SELECT u.username, COUNT(c.Review) AS totalreview
FROM un_users u
LEFT JOIN coin_review c ON c.wallet_address = u.wallet_address
GROUP BY u.username
ORDER BY u.username

我想知道如何获取“最近 24 小时”的记录?

假设review_creation_time是一个unix时间戳,你可以使用UNIX_TIMESTAMP()函数来比较:

SELECT
    u.id, u.wallet_address, u.username,
    COUNT(c.id) AS review_count
FROM un_users u
JOIN coin_review c ON c.wallet_address = u.wallet_address
WHERE c.review_creation_time >  UNIX_TIMESTAMP(CURRENT_TIMESTAMP - INTERVAL 24 HOUR)
AND   c.review_creation_time <= UNIX_TIMESTAMP()
GROUP BY u.id