| Author |
getting last row
|
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
hi Can any one tell me query for getting last row from the table. Thanks
|
-Chiru
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
There are a variety of ways you can do this, all of which are DB dependent. Which database are you using? [ July 18, 2008: Message edited by: Paul Sturrock ]
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
|
MySQL
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
If you are using InnoDB you will have a clustered index on your primary key column. The ordering of your clustered index will match the physical ordering of your data, so the maximum primary key will be the last row. Soemthing like: will get you that.
|
 |
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
|
But my primary key is Date time Stamp i think this max wont work with date time stamp
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
Originally posted by Chiru Raj: But my primary key is Date time Stamp i think this max wont work with date time stamp
Why not? Max should returns the latest date; this is how it behaves on most DBs. Is MySQL different?
|
 |
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
ok thank you. i will check that and tell you.
|
 |
Scott Selikoff
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
Just wondering, but is the purpose of your query to retrieve a row id (or other information) immediately after it's inserted? If so there is a getGeneratedKeys() command that will return the primary key of any records created by a statement. Of course support for this feature is system dependent so test it out before assuming this works. The other approach to get the id of the keys recently inserted without relying on the database is to create the ids of the keys yourself (instead of using the database's key generation feature). In large enough systems where concurrency is an issue, this is often the best approach. [ July 18, 2008: Message edited by: Scott Selikoff ]
|
My Blog: Down Home Country Coding with Scott Selikoff
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
I think in MySQL you use the last_insert_id function for what Scott suggested. Check the documentation.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
|
. . . but last_insert_id only works where you have an auto_increment column.
|
 |
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
Originally posted by Paul Sturrock:
Error is coming my query is [ July 19, 2008: Message edited by: Scott Selikoff ]
|
 |
Chiranjeevi Kanthraj
Ranch Hand
Joined: Feb 18, 2008
Posts: 283
|
|
This is working fine
|
 |
 |
|
|
subject: getting last row
|
|
|