Mysql Select Next & Prev row not order by id

前端 未结 2 1308
星月不相逢
星月不相逢 2021-02-09 07:22

I have a query ordered by NAME that return smt like this:

 ID     NAME
2121927 AAA
2123589 AAB
2121050 AAC
2463926 BBB ---> known ID
2120595 CCC
2122831 DDD
2         


        
2条回答
  •  别跟我提以往
    2021-02-09 07:56

    I did it like this:
    Its not the fastest, but it dont cares about order of fields.
    Query all rows from db in order that you want.
    Use php foreach to find current id.
    Get array index of current id, make index +1 or index -1.

    $current_id = 6;
    $data = get_all_data_from_db();
    foreach($data as $index=>$row) {
    if($current_id == $row['id']) {
    $next = $index+1;
    $prev = $index-1;
    }
    $previous_row = $data[$prev];
    $next_row = $data[$next];

提交回复
热议问题