且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取Oracle表的最后一行

更新时间:2023-01-21 18:44:32

表中没有最后"行之类的东西,因为Oracle表没有顺序概念.

There is no such thing as the "last" row in a table, as an Oracle table has no concept of order.

但是,假设您要查找最后插入的主键并且该主键是一个递增数字,则可以执行以下操作:

However, assuming that you wanted to find the last inserted primary key and that this primary key is an incrementing number, you could do something like this:

select *
  from ( select a.*, max(pk) over () as max_pk
           from my_table a
                )
 where pk = max_pk

如果具有创建每一行的日期,则该日期将变为(如果该列名为created:

If you have the date that each row was created this would become, if the column is named created:

select *
  from ( select a.*, max(created) over () as max_created
           from my_table a
                )
 where created = max_created

或者,您可以使用汇总查询,例如:

Alternatively, you can use an aggregate query, for example:

select *
  from my_table
 where pk = ( select max(pk) from my_table )

这里有一些 SQL小提琴来演示.