MySQL select before after row

前端 未结 4 1326
北荒
北荒 2020-12-15 08:28

This is the example table:

Column             | 1st record | 2nd record | 3rd record | 4th record | etc
id (primary) | 1 | 5
4条回答
  •  长情又很酷
    2020-12-15 08:41

    This works:

    select a.id, a.name, a.date, a.callValue 
    FROM
    (
        select @r0 := @r0 + 1 as rownum, id, name, date, callValue from TABLE
        , (SELECT @r0 := 0) r0
    ) a,
    (
        select @r1 := @r1 + 1 rownum, id, name, date, callValue from TABLE
        , (SELECT @r1 := 0) r1 
    ) b
    where b.callValue = val3 and b.rownum between (a.rownum-1 and a.rownum+1)
    

    It expands the table into 2 dimensions so you can compare the rows in the fist table to any set of rows from the second.

提交回复
热议问题