1

I have a simple question. In MySQL, consider a row "n", how can we order rows by id (for example), but start from the row "n+1" and end to the row "n-1" ?

Thanks !

EDIT : I ommit to precise that I seek the query in MySQL.

From an answer below, here an example :

ID
---
1
2
3
4   <--N
5
6

I want Desired Results ordered as follows

5   <--N + 1
6
1
2
3   <--N - 1
Mehdi Chamouma
  • 97
  • 1
  • 1
  • 8

3 Answers3

1

So you mean. For a table

ID
---
1
2
3
4   <--N
5
6

You want Desired Results ordered as follows?

5   <--N + 1
6
1
2
3   <--N - 1

If so

SELECT ID
FROM T
WHERE ID <> 4
ORDER BY CASE WHEN ID > 4 THEN 0 ELSE 1 END, ID
Martin Smith
  • 438,706
  • 87
  • 741
  • 845
0

Assuming table MyTable with integer column N:

SELECT *
 from MyTable
 where Id between N-1 and N+1
 order by N desc
Philip Kelley
  • 39,426
  • 11
  • 57
  • 92
-1

You're asking how to sort by descending ?

Just stick

ORDER BY col a , col b DESC;

at the end

SELECTCOUNTSTAR
  • 100
  • 2
  • 11