Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting last row

 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Can any one tell me query for getting last row from the table.
Thanks
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MySQL
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But my primary key is Date time Stamp i think this max wont work with date time stamp
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thank you.
i will check that and tell you.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4020
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think in MySQL you use the last_insert_id function for what Scott suggested. Check the documentation.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but last_insert_id only works where you have an auto_increment column.
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:




Error is coming

my query is


[ July 19, 2008: Message edited by: Scott Selikoff ]
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is working fine
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic