title: Next and previous records in mysql after sorting desc
body: <p>I have a simple table called users with the following data:</p>
  
  <pre><code>id | hops
   1 | 3
   2 | 1
   3 | 5
   4 | 2
   5 | 6
   6 | 5
  </code></pre>
  
  <p>I want to make a prev/next navigation according to hops sorted descending. I use the following to query to sort descending:</p>
  
  <pre><code>SELECT * FROM users ORDER BY hops DESC, id DESC
  </code></pre>
  
  <p>This is the result:</p>
  
  <pre><code>id | hops
   5 | 6
   6 | 5
   3 | 5
   1 | 3
   4 | 2
   2 | 1
  </code></pre>
  
  <p>Now what I want is that when I input any id in a mysql query I get the previous and next ids according to sorting above. For example:</p>
  
  <p>For id 5 (In this case id=5 has the highest hops so no previous records before it):</p>
  
  <pre><code>id (current) | hops (current) | id (prev) | hops (prev) | id (next) | hops (next)
     5         |    6           |  NULL     | NULL        |  6        |  5
  </code></pre>
  
  <p>For id 6:</p>
  
  <pre><code>id (current) | hops (current) | id (prev) | hops (prev) | id (next) | hops (next)
     6         |    5           |  5        |     6       |  3        |  5
  </code></pre>
  
  <p>For id 3:</p>
  
  <pre><code>id (current) | hops (current) | id (prev) | hops (prev) | id (next) | hops (next)
     3         |    5           |  6        |    5        |  1        |  3
  </code></pre>
  
  <p>For id 1:</p>
  
  <pre><code>id (current) | hops (current) | id (prev) | hops (prev) | id (next) | hops (next)
     1         |    3           |  3        |    5        |  4        |  2
  </code></pre>
  
  <p>For id 4:</p>
  
  <pre><code>id (current) | hops (current) | id (prev) | hops (prev) | id (next) | hops (next)
     4         |    2           |  1        | 3           |  2        |  1
  </code></pre>
  
  <p>For id 2 (In this case id=2 has the lowest hops so no next records after it)</p>
  
  <pre><code>id (current) | hops (current) | id (prev) | hops (prev) | id (next) | hops (next)
     2         |    1           |  4        | 2           |  NULL     |  NULL
  </code></pre>
  
  <p>Thanks</p>