+ Reply to Thread
Results 1 to 9 of 9

Thread: Retrieving 2nd entry in db query

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Retrieving 2nd entry in db query

    I log each time a user logs in to the database and on the home page show their last login date and time.

    However, from the login page, I save the login details, so I don't want to show the most recent entry on the home page as that will be when they just logged in. I want to show the next most recent entry.

    The tbl has:-
    Code:
    ID field autonumber
    userref field INT
    username field VARCHAR
    login_time DATETIME
    ip_address VARCHAR
    
    So, assuming I had:-
    Code:
    ID     login_time
    1       19/03/2009 08:35
    2       19/03/2009 09:35
    
    The next time I login it will add another entry, say for 10:35. I then want to retrieve the 09:35 entry to show on the home page.

    Is there an easy way to do this?

    I'm thinking of something like SELECT TOP 2, but then I'm not sure how to get the 2nd entry.

    Preferably want to do it using a Stored Proc.

    Any ideas?

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    OK. I got it.Use LIMIT. I forgot you use LIMIT in MySQL, not TOP.

    Code:
    SELECT * FROM tblLogins WHERE userref=@userref ORDER BY loginref DESC LIMIT 1,1
    

  3. #3
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Does this query gives you 2 records RR??
    coz i think it'll give 1 record only.

  4. #4
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by micky View Post
    Does this query gives you 2 records RR??
    coz i think it'll give 1 record only.
    No, it only gives 1 record, but that's all I wanted. I just wanted the 2nd record in the list as the first record will be when they just logged in. So, I don't even need to do anything with moving to the 2nd record or anything.

    I thought I might have to get the first 2 records and then use code to move to the 2nd record...

    All done in one query...

  5. #5
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    ya, u really dont need 2 records from db

    but i wonder how that can be done in a query!!
    anyway, glad u solved it

  6. #6
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by micky View Post
    ya, u really dont need 2 records from db

    but i wonder how that can be done in a query!!
    anyway, glad u solved it
    You mean how to get the first 2 records?
    Something like this:-
    Code:
    SELECT * FROM tblLogins WHERE userref=@userref ORDER BY loginref DESC LIMIT 0,1
    
    I think...To get 2 records starting from the second use LIMIT 1,2

  7. #7
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Quote Originally Posted by richyrich View Post
    You mean how to get the first 2 records?
    no, i mean how to differentiate which is first record and which is second!!

  8. #8
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Quote Originally Posted by micky View Post
    no, i mean how to differentiate which is first record and which is second!!
    Not sure I get you micky.

    It depends on how they're ordered. I just reverse the autonumber ID field as the order by clause. So the most recent is first in the list and next most recent is second in the list.

  9. #9
    Lazy Bum micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky is a jewel in the rough micky's Avatar
    Join Date
    Jul 2008
    Location
    India
    Posts
    1,763
    Blog Entries
    2
    Rep Power
    8

    Quote Originally Posted by richyrich View Post
    Not sure I get you micky.

    It depends on how they're ordered. I just reverse the autonumber ID field as the order by clause. So the most recent is first in the list and next most recent is second in the list.
    hmm, ya ......that might work

+ Reply to Thread

Similar Threads

  1. Time Entry Form
    By sbenj69 in forum Access Database Samples
    Replies: 5
    Last Post: May 1st, 2009, 06:48 AM
  2. Record new entry in a different table
    By gjh in forum Microsoft Access
    Replies: 22
    Last Post: March 4th, 2009, 09:12 PM
  3. Calendar for date entry
    By alex motilal in forum Microsoft Access
    Replies: 1
    Last Post: March 3rd, 2009, 09:59 AM
  4. Combo Box - Add Entry Not In List
    By AOG123 in forum Access Database Samples
    Replies: 0
    Last Post: June 6th, 2008, 10:35 AM
  5. Retrieving Web Data using Excel
    By mehere in forum Microsoft Office
    Replies: 13
    Last Post: May 22nd, 2008, 04:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO