简体   繁体   English

从“ n + 1”行到“ n-1”行的SQL排序数据

[英]SQL Ordering Data from row “n+1” to “n-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" ? 在MySQL中,考虑行“ n”,我们如何按ID对行进行排序(例如),但如何从行“ n + 1”开始到行“ n-1”呢?

Thanks ! 谢谢 !

EDIT : I ommit to precise that I seek the query in MySQL. 编辑:我省略精确地我在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

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

Assuming table MyTable with integer column N : 假设表MyTable的整数MyTable N

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

You're asking how to sort by descending ? 您在问如何按降序排序?

Just stick 只是坚持

ORDER BY col a , col b DESC;

at the end 在末尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM