Getting the previous and next record in MySQL when ordering by date and providing duplicate dates

I tried to find other help for this problem, but I just do not understand 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  |
+----+--------------+------------+

Upon request, I order these records in accordance with date_coland use id(or indeed any other arbitrary column) to order with duplicate dates.

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

, , . , date_col , , , . (, this_date - date_col, this_id - id , )

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

:

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");

, , - : β„–4, date_col DESC id DESC, β„– 5, id # 3, .

- , ? .

+3
2

PREV NEXT, . , date_col , .

, NEXT (id) " , ". , : PREV (NEXT (id)) id . kludges, .

?

,

SELECT id FROM table WHERE id = current_id +1

, ,

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

,

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

, date_col. PREV.

+3

( ), , ALL , , , . , , , , , .

0

Source: https://habr.com/ru/post/1763678/


All Articles