Getting the last row

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting the last row

Aravinda babu-2
Hi all,

Is there any easy way to get the last row in the table ?

Thanks,
Aravind.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Getting the last row

Chris Wedgwood
On Wed, Sep 17, 2008 at 11:44:38AM +0530, Aravinda babu wrote:

> Is there any easy way to get the last row in the table ?

what do you mean by last?  last by row id?

then

    select * from t1 where oid = (select max(oid) from t1);

should work
_______________________________________________
sqlite-users mailing list
[hidden email]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Getting the last row

Trey Mack
In reply to this post by Aravinda babu-2
select * from t order by rowid desc limit 1;

Thanks,
Trey Mack

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Aravinda babu
Sent: Wednesday, September 17, 2008 2:15 AM
To: [hidden email]
Subject: [sqlite] Getting the last row

Hi all,

Is there any easy way to get the last row in the table ?

Thanks,
Aravind.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

No virus found in this incoming message.
Checked by AVG - http://www.avg.com 
Version: 8.0.169 / Virus Database: 270.6.21/1675 - Release Date: 9/16/2008
7:06 PM

_______________________________________________
sqlite-users mailing list
[hidden email]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Getting the last row

Darren Duncan
In reply to this post by Aravinda babu-2
Aravinda babu wrote:
> Is there any easy way to get the last row in the table ?

What do you mean by "last row"?

Do you mean you want to get back the row that was most recently inserted?

If so, then the table needs to have a column containing info about
insertion order of rows, such as a serial number that is guaranteed to
always increase and not repeat, or such as a timestamp.

If you had a column like that, say name it 'myorder', then you could use a
query like this to get the last row:

   select * from mytable
   where myorder IN (select max(myorder) from mytable)

If you mean get the row that would sort last in a query, then you have
something like this at the end of the query, after the "ORDER BY":

   LIMIT 1 OFFSET (select count(*) from mytable) - 1

Or better yet, reverse your ORDER BY condition / make it DESC, in which
case you can then just return the *first* row:

   LIMIT 1

Note that rows in tables are not ordered (a table is a set of rows), so
there is no concept of newly inserted ones going at the end.

-- Darren Duncan
_______________________________________________
sqlite-users mailing list
[hidden email]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users