How to get the last row of an Oracle a table

后端 未结 7 2217
名媛妹妹
名媛妹妹 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:08
    select * from table_name ORDER BY primary_id DESC FETCH FIRST 1 ROWS ONLY;
    

    That's the simplest one without doing sub queries

    0 讨论(0)
  • 2020-11-28 09:09

    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
    

    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 )
    

    Here's a little SQL Fiddle to demonstrate.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 09:21
    $sql = "INSERT INTO table_name( field1, field2 )  VALUES ('foo','bar') 
            RETURNING ID INTO :mylastid";
    $stmt = oci_parse($db, $sql);
    oci_bind_by_name($stmt, "mylastid", $last_id, 8, SQLT_INT);
    oci_execute($stmt);
    
    echo "last inserted id is:".$last_id;
    

    Tip: you have to use your id column name in {your_id_col_name} below...

    "RETURNING {your_id_col_name} INTO :mylastid"
    
    0 讨论(0)
  • 2020-11-28 09:23
    SELECT * FROM (
        SELECT * FROM table_name ORDER BY sortable_column DESC
    ) WHERE ROWNUM = 1;
    
    0 讨论(0)
  • 2020-11-28 09:24

    You can do it like this:

    SELECT * FROM (SELECT your_table.your_field, versions_starttime
                   FROM your_table
                   VERSIONS BETWEEN TIMESTAMP MINVALUE AND MAXVALUE)
    WHERE ROWNUM = 1;
    

    Or:

    SELECT your_field,ora_rowscn,scn_to_timestamp(ora_rowscn) from your_table WHERE ROWNUM = 1;
    
    0 讨论(0)
提交回复
热议问题