0

我的问题可能没有最好的标题,但这就是我的意思。我有这个结果,它是在使用几个连接查询后产生的。

 id  | owner_id     |   name    |        order | count 
-----+--------------+-----------+--------------+-------
 274 |        25041 | first     |            1 |     0
 269 |        25041 | second    |            2 |     2
 275 |        25041 | third     |            3 |     0
 276 |        25041 | fourth    |            4 |     0
 273 |        25041 | fifth     |            5 |     1
 277 |        25041 | sixth     |            6 |     0

我需要一个查询,使用上面的 order 列添加一个具有下一个名称的列。它应该循环,它应该在第六个之后说在第一个之后。

 id  | owner_id     |   name    |        order | count |   next    
-----+--------------+-----------+--------------+-------+-----------
 274 |        25041 | first     |            1 |     0 | second
 269 |        25041 | second    |            2 |     2 | third
 275 |        25041 | third     |            3 |     0 | fourth
 276 |        25041 | fourth    |            4 |     0 | fifth
 273 |        25041 | fifth     |            5 |     1 | sixth
 277 |        25041 | sixth     |            6 |     0 | first
4

1 回答 1

1

尝试这个

select t1.*
,CASE WHEN t2.name IS NULL THEN 'first' ELSE t2.name END as next 
from  Table1 as t1
LEFT join Table1 as t2 on t1.order = t2.order-1

SQL 小提琴演示

于 2013-10-10T16:10:36.717 回答