簡體   English   中英

排序desc后,mysql中的下一個和前一個記錄

[英]Next and previous records in mysql after sorting desc

我有一個名為users的簡單表,其中包含以下數據:

id | hops
 1 | 3
 2 | 1
 3 | 5
 4 | 2
 5 | 6
 6 | 5

我想根據跳躍排序降序進行上一個/下一個導航。 我使用以下查詢來降序排序:

SELECT * FROM users ORDER BY hops DESC, id DESC

這是結果:

id | hops
 5 | 6
 6 | 5
 3 | 5
 1 | 3
 4 | 2
 2 | 1

現在我想要的是,當我在mysql查詢中輸入任何id時,我會根據上面的排序得到上一個和下一個id。 例如:

對於id 5(在這種情況下,id = 5具有最高的躍點,因此之前沒有先前的記錄):

id (current) | hops (current) | id (prev) | hops (prev) | id (next) | hops (next)
   5         |    6           |  NULL     | NULL        |  6        |  5

對於id 6:

id (current) | hops (current) | id (prev) | hops (prev) | id (next) | hops (next)
   6         |    5           |  5        |     6       |  3        |  5

對於身份3:

id (current) | hops (current) | id (prev) | hops (prev) | id (next) | hops (next)
   3         |    5           |  6        |    5        |  1        |  3

對於id 1:

id (current) | hops (current) | id (prev) | hops (prev) | id (next) | hops (next)
   1         |    3           |  3        |    5        |  4        |  2

對於id 4:

id (current) | hops (current) | id (prev) | hops (prev) | id (next) | hops (next)
   4         |    2           |  1        | 3           |  2        |  1

對於id 2(在這種情況下,id = 2具有最低的跳數,因此之后沒有下一個記錄)

id (current) | hops (current) | id (prev) | hops (prev) | id (next) | hops (next)
   2         |    1           |  4        | 2           |  NULL     |  NULL

謝謝

嘗試:

select cp.*, n.id id_next, n.hops hops_next
from
(select c.id id_current, c.hops hops_current, p.id id_previous, p.hops hops_previous
 from
 (select * from users where id = ?) c
 left join users p on c.hops < p.hops or (c.id < p.id and c.hops = p.hops)
 order by p.hops, p.id limit 1) cp 
left join users n 
       on cp.hops_current > n.hops or (cp.id_current > n.id and cp.hops_current = n.hops)
order by n.hops desc, n.id desc limit 1

(SQLFiddle 這里

這是迄今為止我見過的最奇怪的用戶表。 無論如何這里是一種方式(雖然我不得不承認它有點復雜)......

DROP TABLE IF EXISTS test;

CREATE TABLE test(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,hops INT NOT NULL);

INSERT INTO test VALUES
(1 ,3),(2 ,1),(3,5),(4 ,2),(5 ,6),(6 ,5);

SELECT c.id id_curr
     , c.hops hops_curr
     , p.id id_prev
     , p.hops hops_prev
     , n.id id_next
     , n.hops hops_next
  FROM
     (
       SELECT a.*
            , COUNT(*) new_rank
         FROM 
            ( SELECT x.*
                   , COUNT(*) rank 
                FROM test x 
                JOIN test y 
                  ON y.hops >= x.hops 
               GROUP 
                  BY x.id
            ) a
         JOIN 
            ( SELECT x.*
                   , COUNT(*) rank 
                FROM test x 
                JOIN test y 
                  ON y.hops >= x.hops 
               GROUP 
                  BY x.id
            ) b
           ON b.rank < a.rank
           OR (b.rank = a.rank AND b.id >= a.id)
        GROUP 
           BY a.id
     )c
  LEFT
  JOIN
     (
       SELECT a.*
            , COUNT(*) new_rank
         FROM 
            ( SELECT x.*
                   , COUNT(*) rank 
                FROM test x 
                JOIN test y 
                  ON y.hops >= x.hops 
               GROUP 
                  BY x.id
            ) a
         JOIN 
            ( SELECT x.*
                   , COUNT(*) rank 
                FROM test x 
                JOIN test y 
                  ON y.hops >= x.hops 
               GROUP 
                  BY x.id
            ) b
           ON b.rank < a.rank
           OR (b.rank = a.rank AND b.id >= a.id)
        GROUP 
           BY a.id
     ) p
    ON p.new_rank = c.new_rank-1
  LEFT
  JOIN
     (
       SELECT a.*
            , COUNT(*) new_rank
         FROM 
            ( SELECT x.*
                   , COUNT(*) rank 
                FROM test x 
                JOIN test y 
                  ON y.hops >= x.hops 
               GROUP 
                  BY x.id
            ) a
         JOIN 
            ( SELECT x.*
                   , COUNT(*) rank 
                FROM test x 
                JOIN test y 
                  ON y.hops >= x.hops 
               GROUP 
                  BY x.id
            ) b
           ON b.rank < a.rank
           OR (b.rank = a.rank AND b.id >= a.id)
        GROUP 
           BY a.id
     ) n
    ON n.new_rank = c.new_rank+1
 ORDER 
    BY c.hops DESC
     , c.id DESC;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM