Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1043437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:40:00+00:00 2026-05-16T15:40:00+00:00

I’ve tried looking up other help for this problem but I’m just not getting

  • 0

I’ve tried looking up other help for this problem but I’m just not getting it. Suppose I have a table that looks like the one below.

+----+--------------+------------+
| id | date_col     | label      |
+----+--------------+------------+
| 1  | 2010-09-07   | Record 1   |
| 2  | 2010-09-03   | Record 2   |
| 3  | 2010-08-23   | Record 3   |
| 4  | 2010-08-23   | Record 4   |
| 5  | 2010-08-23   | Record 5   |
| 6  | 2010-08-12   | Record 6   |
| 7  | 2010-08-06   | Record 7   |
| 8  | 2010-08-06   | Record 8   |
| 9  | 2010-08-02   | Record 9   |
| 10 | 2010-08-01   | Record 10  |
+----+--------------+------------+

When queried, I order these records according to date_col and use id (or really any other arbitrary column) to help ordering with duplicate dates.

mysql_query("SELECT * FROM table ORDER BY date_col DESC, id DESC");

However, when I query only one of these records at a time, I want to have a previous and a next button to navigate to the next or previous record. My problem is that date_col allows duplicate values and so, for example, the query below does not work for me when determining the next record in sequence. (assume that this_date is the date_col value and this_id is the id value for the current record we are looking at)

mysql_query("SELECT id FROM table WHERE date_col > this_date ORDER BY date_col DESC, id DESC LIMIT 1");

Even this wouldn’t work for me:

mysql_query("SELECT id FROM table WHERE date_col > this_date AND NOT id=this_id ORDER BY date_col DESC, id DESC LIMIT 1");

So what I’m going for is something like this – If I’m looking at the record with id #4, since its being ordered by date_col DESC id DESC, the previous record should be id #5 and the next record should be id #3, but I’m not getting these results at all.

Can someone explain how to make this work properly? Any help is much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:40:00+00:00Added an answer on May 16, 2026 at 3:40 pm

    If you’re going to have workable PREV and NEXT buttons in your view, you’re going to need a solid conceptual foundation for record ordering in your data model. As you have noticed, your date_col doesn’t do the trick because it permits duplicates.

    It seems like your concept of NEXT(id) means “the row with the smallest ID number whose ID number is greater than the current id number and whose date is greater than the current row’s date.” That’s OK, but it has a flaw: PREV(NEXT(id)) isn’t necessarily equal to id in all cases. This may drive your users around the bend, and may drive your program logic into some real kludges to get things to work.

    Why not simply use the serial number?

    To get the “next” row, either do

    SELECT id FROM table WHERE id = current_id +1
    

    or, if your ID numbers are not guaranteed to be contiguous, do

    SELECT min(id) AS id FROM table WHERE id > current_id
    

    If you must show the next date instead of the next row with your next button, then do

    SELECT id, date_col
      FROM table 
     WHERE id = (   
      SELECT min(id) AS id
        FROM table 
       WHERE date_col > this_date
    )
    

    But if you do that be sure to create an index for date_col. There are equivalent versions of all these queries to use for your PREV button.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.