IT Community - Software Programming, Web Development and Technical Support

how to get the last record only?

This is a discussion on how to get the last record only? within the Database Support forums, part of the Web Development category; This is my Example table structure code empname salary A01 S1 7500 A02 S2 5500 A00 S3 6500 how to ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 08-13-2007, 04:19 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post how to get the last record only?

This is my Example table structure

code empname salary
A01 S1 7500
A02 S2 5500
A00 S3 6500


how to get the last record only.i will take the single query. pls help me.

Note:All Database(Access,oracle,sql server,MySQL)
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-13-2007, 05:06 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: how to get the last record only?

In MYSQL

Code:
SELECT * FROM <TableName> ORDER BY <TablePrimaryKeyField> DESC LIMIT 1;
In ORACLE


Code:
SELECT * FROM <TableName> WHERE <TablePrimaryKeyField> =(SELECT MAX(TablePrimaryKeyField> FROM <TableName>);
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-13-2007, 07:10 AM
write2ashokkumar write2ashokkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 85
write2ashokkumar is on a distinguished road
Thumbs up Re: how to get the last record only?

hi...

For Mysql, consider the table name is SAMPLE.

Query to get the last record of the table:

Method 1:

SELECT * FROM SAMPLE ORDER BY _ROWID DESC LIMIT 1;

Method 2:

SELECT * FROM SAMPLE WHERE _ROWID = (SELECT MAX(_ROWID) FROM SAMPLE);

Note : _ROWID is keyword

Regards,
S.Ashokkumar

Last edited by write2ashokkumar : 08-13-2007 at 07:14 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-13-2007, 08:39 AM
KiruthikaSambandam KiruthikaSambandam is offline
D-Web Analyst
 
Join Date: Aug 2007
Posts: 332
KiruthikaSambandam is on a distinguished road
Default Re: how to get the last record only?

Hi,

Use the top and order by.

for example,

select top 1 * from table order by field desc

with regards,
KiruthikaSambandam
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-14-2007, 01:14 AM
kumaresan kumaresan is offline
D-Web Trainee
 
Join Date: Jul 2007
Posts: 12
kumaresan is on a distinguished road
Send a message via AIM to kumaresan Send a message via Yahoo to kumaresan
Exclamation Re: how to get the last record only?

hi,

There is another way in Mysql to get the last record

HANDLER <tablen-name> OPEN AS <alias>;
HANDLER <alias> READ `primary` last;
handler <alias> close ;


Thanks,
V.Kumaresan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-14-2007, 01:31 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: how to get the last record only?

hi,
is there any way to get the nth record in mysql?

for example i want to get 3 record from the total of 100 records.
is there any generic way to find it in MySql?
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-14-2007, 03:44 AM
kumaresan kumaresan is offline
D-Web Trainee
 
Join Date: Jul 2007
Posts: 12
kumaresan is on a distinguished road
Send a message via AIM to kumaresan Send a message via Yahoo to kumaresan
Post Re: how to get the last record only?

Hi ,

To get the N-th row from table in Mysql

SET @N=3; -- for 3rd row from the table
SET @M=@N-1;
SET @S:='SELECT * FROM <Table_Name> LIMIT ?,1';
PREPARE STM FROM @S;
EXECUTE STM USING @M;



--------------
Thanks,
V.Kumaresan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-14-2007, 04:13 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: how to get the last record only?

Quote:
Originally Posted by kumaresan View Post
hi,

There is another way in Mysql to get the last record

HANDLER <tablen-name> OPEN AS <alias>;
HANDLER <alias> READ `primary` last;
handler <alias> close ;


Thanks,
V.Kumaresan.
Could you please clarify the MYSQL Version HANDLER is available?

Got it thanks...
__________________
-Murali..

Last edited by Murali : 08-14-2007 at 04:20 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-24-2007, 11:39 PM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Default Re: how to get the last record only?

hii guys,

Thanks For
Murali
Ashokkumar
kumaresan
Jeyaseelansarc
KiruthikaSambandam

your answer post.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i lock a record id using JSP? saravanan Java Server Pages (JSP) 0 03-19-2008 10:03 PM
Audio Record in ASP.NET kingmaker ASP and ASP.NET Programming 12 01-08-2008 05:45 AM
How To Record A Phone Conversation With A PC vadivelanvaidyanathan Computer Hardware 1 09-18-2007 08:39 AM
Different record methods Shanthi Testing Tools 1 08-27-2007 05:50 AM
Difference between ADO.NET dataset and ADO Record set? prasath ASP and ASP.NET Programming 1 07-18-2007 08:06 AM


All times are GMT -7. The time now is 06:39 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0