How to get the last row of an Oracle a table

后端 未结 7 2261
名媛妹妹
名媛妹妹 2020-11-28 08:31

I want to get the last row, which I inserted into a table in an Oracle 11g Express database. How can I do this?

7条回答
  •  悲&欢浪女
    2020-11-28 09:18

    The last row according to a strict total order over composite key K(k1, ..., kn):

    SELECT  *
    FROM    TableX AS o
    WHERE   NOT EXISTS (
                SELECT  *
                FROM    TableX AS i
                WHERE   i.k1 > o.k1
                    OR  (i.k1 = o.k1 AND i.k2 > o.k2)
                    ...
                    OR  (i.k1 = o.k1 AND i.k2 = o.k2 AND i.k3 = o.k3 AND ... AND i.kn > o.kn)
            )
    ;
    

    Given the special case where K is simple (i.e. not composite), the above is shortened to:

    SELECT  *
    FROM    TableX AS o
    WHERE   NOT EXISTS (
                SELECT  *
                FROM    TableX AS i
                WHERE   i.k1 > o.k1
            )
    ;
    

    Note that for this query to return just one row the key must order without ties. If ties are allowed, this query will return all the rows tied with the greatest key.

提交回复
热议问题