SQL Ordering Data from row “n+1” to “n-1”

混江龙づ霸主 提交于 2019-12-24 17:22:24

问题


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

回答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



回答2:


Assuming table MyTable with integer column N:

SELECT *
 from MyTable
 where Id between N-1 and N+1
 order by N desc



回答3:


You're asking how to sort by descending ?

Just stick

ORDER BY col a , col b DESC;

at the end



来源:https://stackoverflow.com/questions/17114876/sql-ordering-data-from-row-n1-to-n-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!