从表中选择最后修改的数据记录

Posted

技术标签:

【中文标题】从表中选择最后修改的数据记录【英文标题】:Select last Modified data record from the Table 【发布时间】:2021-09-30 09:43:53 【问题描述】:

这是桌子的样子。我只想选择 Last Modified Date 为 Max 的记录。 EX:只会选择上表中的第二条记录。 有可能吗?

【问题讨论】:

这似乎与您的问题相同。看看这里***.com/questions/2411559/… 【参考方案1】:

如果即使最大值出现多次,您也只想要一行,请使用 LIMIT:

select amount, created_date, last_mod_date
from the_table
order by last_mod_date desc
limit 1;

如果最大值出现不止一次,你想要多行,你可以使用窗口函数:

select amount, created_date, last_mod_date
from (
    select amount, created_date, last_mod_date, 
           dense_rank() over (order by last_mod_date desc) as rn
    from the_table
) t 
where rn = 1;

【讨论】:

【参考方案2】:

使用 order by 和 limit

select a.* from table_name a
order by last_mod_date desc
limit 1

【讨论】:

以上是关于从表中选择最后修改的数据记录的主要内容,如果未能解决你的问题,请参考以下文章