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

后端 未结 5 961
独厮守ぢ
独厮守ぢ 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条回答
  •  [愿得一人]
    2020-12-18 08:19

    yes possible using temp variable

    Example :

    set @a := 0;
    select * from car_m_city  WHERE mod((@a:=@a+1), 2) = 1
    

    Explanation :

    here in sql we declare @a( set @a := 0;) temp variable.(@a:=@a+1) now @a increment by 1.jsut like simple way to check odd or even

    mod((@a:=@a+1), 2) = 1 for odd data

    mod((@a:=@a+1), 2) = 0 for even data

提交回复
热议问题