简体   繁体   中英

Get next and previous records sorting by non-unique field

I was not able to find an answer while reading similar questions, so I'll ask my question here, appreciate any help on this matter.

In MySQL DB have a table with articles:

id        date     is_active                title                  text    
 3     2017-01-20      1     New payment system goes live       some articl
 5     2017-01-21      1     Library v.2.5 released             some articl
 6     2017-01-22      1     New skins and themes               some articl
 7     2017-01-25      0     Terms and Conditions updated       some articl
 8     2017-01-26      1     Don't forget to subscribe          some articl
10     2017-01-22      0     Support Chat beta release          some articl
11     2017-01-30      1     Maintenance window next Sunday     some articl
12     2017-01-28      1     Refer a friend and get a bonus     some articl
13     2017-01-26      1     Follow us in social networks       some articl
14     2017-01-22      1     Video sharing feature is now live  some articl

I have 2 web pages:

  1. a list of all active articles ordered by date (important: it is possible that several articles can have same date).

  2. single article read page, additionally I have "Next" and "Previous" links on that page

I would like to show 'next' and 'prev' links as a href="article.php?id=8". So I would like to get next record ID from DB according to the query like this:

SELECT id FROM articles WHERE date > (SELECT date FROM news WHERE id = 6) and isactive = 1 ORDER BY date ASC LIMIT 1;

However my query does not work properly when it comes to defining next article id with the same date. When user stays on the article id=3 and clicks 'next' multiple times I would expect the following order of loading articles:

id        title      date     is_active     
 5     Library v.2 2017-01-21      1     
 6     New skins a 2017-01-22      1     
14     Video shari 2017-01-22      1     
 8     Don't forge 2017-01-26      1     
13     Follow us i 2017-01-26      1     
12     Refer a fri 2017-01-28      1     
11     Maintenance 2017-01-30      1     

But what I get is: 5, 6, 8, 12, 11. So 2 records are missing in this sequence (ids: 14 and 13) because they have same date. I tried playing with different queries, but all results are not 100% right (in some cases it keeps returning same numbers, ex.: 5, 6, 14, 6, 14....., etc. )

Is is possible to get the 'next' IDs based on current ID and desired order via MySQL query(ies)? I am not against doing several queries or working with nested queries.

As a workaround, of course I can just retrieve an ordered array of all ids like this:

SELECT id FROM articles WHERE isactive=1 ORDER BY date

and then define 'next' and 'previous' using this array, however I don't like this solution. If you can point me to some similar topics or other possible ways to solve this, please do. Thank you.

 SELECT id
 FROM   articles
 WHERE  date >= (SELECT date -- changed
           FROM   news
           WHERE  id = 6)
          AND isactive = 1
          AND id NOT IN(6) -- added, maybe id!=6 or id<>6
 ORDER  BY date ASC
 LIMIT  1  
-- (all id date >= date) - (id=6)

Why you do this, why not use
SELECT id FROM news ORDER BY date ASC LIMIT 6,1 -- possition, count
If SQL query caching, this is may be faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM