仅获取oracle sql中最新的行

时间:2016-09-30 01:52:54

标签: sql oracle

我试过简单

select date_created from smc_log_messages where rownum =1
order by date_created desc

并返回类似

的日期
15-SEP-16 10.15.49.099000000 PM

然而,当我跑

 select date_created from smc_log_messages 
    order by date_created desc

我看到像

这样的数据
30-SEP-16 12.39.00.006000000 AM
30-SEP-16 12.38.59.997000000 AM

因此,基本上添加rownum会影响结果。我做错了吗?

2 个答案:

答案 0 :(得分:9)

如果您想要最近的日期,请使用:

select max(date_created)
from smc_log_messages ;

如果您想要Oracle 12C +中的最新行:

select lm.*
from smc_log_messages lm
order by lm.date_created desc
fetch first 1 row only;

在早期版本中:

select lm.*
from (select lm.*
      from smc_log_messages lm
      order by lm.date_created desc
     ) lm
where rownum = 1;

答案 1 :(得分:0)

仅使用MAX功能获取最新和最高记录: SELECT MAX() 来自

这将有效。

相关问题