1

我正在寻找从昨天开始的最大时间戳。将数据存储为日期时间。

数据库布局是表名:收益

列:id、收入、时间戳

目前正在执行命令:

SELECT earnings, timestamp FROM earnings WHERE timestamp = (SELECT MAX(timestamp) FROM earnings)

现在我想得到昨天的最高时间戳......请让我知道通过 mySQL 或 PHP 和 mySQL 执行此操作的最佳方法。

4

2 回答 2

3
SELECT earnings, timestamp 
FROM earnings 
WHERE timestamp = (SELECT MAX(timestamp) 
                   FROM earnings
                   WHERE DATE(timestamp) = DATE_ADD(CURDATE(),INTERVAL -1 DAY))

或者

SELECT  a.*
FROM    earnings a
        INNER JOIN
        (
            SELECT MAX(timestamp) max_Date
            FROM earnings
            WHERE DATE(timestamp) = DATE_ADD(CURDATE(),INTERVAL -1 DAY)
        ) b ON a.timestamp = b.max_Date
于 2013-01-18T05:41:40.233 回答
0

为什么大家都这么喜欢子选择?

SELECT * FROM earnings ORDER BY timestamp DESC limit 1

对于您的第一个查询,以及

SELECT * FROM earnings WHERE timestamp < CURDATE() ORDER BY timestamp DESC limit 1

第二个

于 2013-01-18T05:48:47.970 回答