我的问题可能没有最好的标题,但这就是我的意思。我有这个结果,这是在一个带有几个连接的查询后产生的。
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
我需要一个查询,用于添加具有下一个名称的列,具体取决于使用上面的订单列。应该循环它应该说在第六个跟随第一个之后。
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
答案 0 :(得分: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