Select every other row in MySQL without any identifier?
This should work for MySQL:
SELECT col1, col2
FROM (
SELECT col1, col2, @rowNumber:=@rowNumber+ 1 rn
FROM YourTable
JOIN (SELECT @rowNumber:= 0) r
) t
WHERE rn % 2 = 1
In this case,% is used, which is the MOD operator.
And here is a sample script: http://sqlfiddle.com/#!2/cd31b/2
source to share
Try it. I adapted it from the answer below. I tested it on SQLFiddle and it seems to work.
http://sqlfiddle.com/#!2/0bccf/28
http://sqlfiddle.com/#!2/0bccf/29
Odd lines:
SELECT x.*
FROM (
SELECT @rownum:=@rownum+1 rownum, t.*
FROM (SELECT @rownum:=0) r, table t
) x
WHERE MOD(x.rownum, 2) = 1
Even lines:
SELECT x.*
FROM (
SELECT @rownum:=@rownum+1 rownum, t.*
FROM (SELECT @rownum:=0) r, table t
) x
WHERE MOD(x.rownum, 2) = 0
Adapted from: MySQL Row Number
source to share
Consider this related answer: How to show only even or odd rows in SQL Server 2008?
It seems that you are doing exactly what you want. I think this works in MySql, but I'm not sure.
source to share
yes it is possible to use a temporary variable
Example:
set @a := 0;
select * from car_m_city WHERE mod((@a:=@a+1), 2) = 1
Explanation:
here in sql we are declaring temp @a ( set @a := 0;
) variable . (@a:=@a+1)
now @a
increment on 1.jsut as a simple way to check for odd or even
mod((@a:=@a+1), 2) = 1
for odd data
mod((@a:=@a+1), 2) = 0
for even data
source to share