你如何从现有表中调用最新日期?

时间:2018-03-02 17:18:13

标签: sql date

我有一个名为tbl.date的表格,我有一个列LAST_UPDATE。我需要获取最新的更新日期。这就是我到目前为止所做的:

SELECT *
FROM [tbl.date]
WHERE [LAST_UPDATE] = --I need an SQL system code to go in here to fetch the latest update date

3 个答案:

答案 0 :(得分:1)

如果您使用的是SQL Server,并且LAST_UPDATE列是日期类型

SELECT * FROM tbl.date
WHERE LAST_UPDATE = (SELECT MAX(LAST_UPDATE) FROM tbl.date)

答案 1 :(得分:0)

我只会SELECT TOP 1然后ORDER BY DESC

SELECT TOP 1 *
FROM [tbl.date]
ORDER BY [LAST_UPDATE] DESC

OR

SELECT MAX(LAST_UPDATE) AS LastUpdate
FROM [tbl.date]

OR

SELECT [LAST_UPDATE]
FROM [tbl.date]
ORDER BY [LAST_UPDATE] DESC
LIMIT 1;

答案 2 :(得分:0)

另一种方法,使用TOP WITH TIES:

SELECT TOP (1) WITH TIES *
    FROM  tbl.date
    ORDER BY [LAST_UPDATE] DESC;

它返回表中与最新更新日期匹配的所有记录。