mySQL查询昨天的最大时间戳

时间:2013-01-18 05:40:37

标签: php mysql sql database mysqli

我希望从昨天开始获得最大时间戳。将数据存储为日期时间。

Db布局是表格名称:收入

Coloumns:id,收入,时间戳

目前正在执行命令:

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

现在我希望获得昨天最高的时间戳....请通过mySQL或PHP和mySQL告诉我这样做的最佳方法。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

为什么每个人都喜欢亚选择这么多?

SELECT * FROM earnings ORDER BY timestamp DESC limit 1

第一次查询,

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

代表第二个