MySQL - next / previous ID wih cycling

前端 未结 1 1714
北海茫月
北海茫月 2020-12-20 08:31

I need to create previous / next functionality with cycling. My current solution, If I select MAX(ID) gives next as NULL.

How can I most e

相关标签:
1条回答
  • 2020-12-20 08:52

    for next and previous id for the current foto you can do like this

    SELECT COALESCE((SELECT id FROM `foto` WHERE id<'$this->id' ORDER BY DESC id LIMIT 1),
           (SELECT  MAX(id) FROM `foto`))  AS `prev`,
           COALESCE((SELECT id FROM `foto` WHERE id>'$this->id' ORDER BY id LIMIT 1 ),
           (SELECT MIN(id) FROM `foto`)) AS `next`
    

    Hope it works fine for your needs

    0 讨论(0)
提交回复
热议问题