Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select only even/odd rows in MySQL [duplicate]

Tags:

sql

select

mysql

I'm trying to select all even or odd rows from a table in MySQL without using the ID field. I tried this, but I suppose that it doesn't work since it's based on SQL Server: how to show only even or odd rows in sql server 2008?

Thank you all in advance.

like image 799
AleVale94 Avatar asked Dec 11 '22 03:12

AleVale94


1 Answers

Assuming you have a column that specifies the ordering of the table, then you can use variables to do what you want:

select t.*
from (select t.*, (@rn := @rn + 1) as seqnum
      from table t cross join
           (select @rn := 0) vars
      order by col
     ) t
where mod(seqnum, 2) = 0;
like image 181
Gordon Linoff Avatar answered Dec 28 '22 01:12

Gordon Linoff