dsdqpdjpq16640651 2019-05-14 20:28
浏览 227

获取mysql查询中的下一行

Let's say I have a query: SELECT * FROM table In this scenario I could get the id and and -1 or +1 the id to get the next or previous row.

But if I have SELECT * FROM table WHERE name LIKE ..... I would get several rows and they wouldn't be order by id, how would i be able to get the next or previous row?

My objective is creating a galleri that has a next and previous button with a link to the next or previous image.

This galleri can be ordered and sorted in many different ways.

Is it possible to detect which row is the next or previous in this scenario?

  • 写回答

3条回答 默认 最新

  • dongshi3818 2019-05-14 20:48
    关注

    You must add the column called "GalleryID" or something like this and get the GalleryID from searched query than select all photos by GalleryID and sort them by ID, Now you have the list of sorted photos, e.g:

    $queryResult = $db->query("SELECT GalleryID FROM table WHERE name LIKE '%WHAT I WANT%'")->fetchall();
    $GalleryID = $queryResult[0]['GalleryID'];
    $photos = $db->query("SELECT ID FROM table WHERE GalleryID = $GalleryID order by ID asc")->fetchall();
    

    Now you can play with INDEX;

    $index = 0; // Current photo index
    //Get next photo
    $index++;
    if($index < count($photos))
        $photoUrl = $db->query("select url from table where ID = $photos[$index] limit 1");
    
    // Get the previous photo
    $index--;
    if($index >= 0)
        $photoUrl = $db->query("select url from table where ID = $photos[$index] limit 1");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)