select every other row in MySQL without depending on any ID?

后端 未结 5 963
独厮守ぢ
独厮守ぢ 2020-12-18 07:36

Considering following table that doesn\'t have any primary key, can I select every other row?

col1      col2
 2         a
 1         b
 3         c
 12               


        
5条回答
  •  Happy的楠姐
    2020-12-18 08:40

    In unique MySQL fashion:

    select  *
    from    (
            select  *
            ,       @rn := @rn + 1 as rn
            from    Table1
            join    (select @rn := 0) i
            ) s
    where   rn mod 2 = 0 -- Use = 1 for the other set
    

    Example at SQL Fiddle.

提交回复
热议问题