Getting row before and after a query

前端 未结 5 2025
失恋的感觉
失恋的感觉 2021-01-16 20:15

I currently have this query

SELECT  short_url,
        type,
        file_thumbnail,
        file_embed,
        media.id,
        user,
        media.file_u         


        
5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-16 21:05

    You could use a union to get the previous and next rows:

    SELECT
        ...
    FROM info
    INNER JOIN media
        ON info.mid = media.id
    WHERE media.id < '$id'
    GROUP BY mid
    ORDER BY media.id DESC
    UNION ALL
    SELECT
        ...
    FROM info
    INNER JOIN media
        ON info.mid = media.id
    WHERE media.id = '$id'
    GROUP BY mid
    ORDER BY media.id DESC
    UNION ALL
    SELECT
        ...
    FROM info
    INNER JOIN media
        ON info.mid = media.id
    WHERE media.id > '$id'
    GROUP BY mid
    ORDER BY media.id ASC
    

    However, if your user will want to see the previous item or next item each time they navigate, then you're still going to hit the database to get the next item in whatever direction they're navigating.

    I would recommend pulling a page at a time, and navigating through it on the front end instead.

提交回复
热议问题