MySQL join table to itself based on row number returns no data

前端 未结 1 1182
清酒与你
清酒与你 2021-01-24 20:36

I\'ve got a logs table with an ID column in it, and I want to do the following:

  • Select the ID and row number
  • Join the table to itself based on the row num
相关标签:
1条回答
  • 2021-01-24 21:14

    Change your variable name in second query, using same variable in both queries can produce unexpected results

    select * from 
        (select id, @rownum := @rownum + 1 as 'row' 
            from logs, (select @rownum := 0) r order by id) a
    join
        (select id, @rownum1 := @rownum1 + 1 as 'row1' 
            from logs, (select @rownum1 := 0) r order by id) b
    on a.row = b.row1;
    
    0 讨论(0)
提交回复
热议问题