increment - MySQL: Strange WHERE behaviour with incrementing variables -


i have table 16 sequential rows:

create table t (     id int not null primary key auto_increment ); insert t values (),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(); 

executing following statement, expected, returns rows:

mysql> select * t cross join (select @x:=0) id=(@x:=@x+1); +----+-------+ | id | @x:=0 | +----+-------+ |  1 |     0 | |  2 |     0 | |  3 |     0 | ... 

however, following statement, rather returning every other row, returns nothing:

mysql> select * t cross join (select @x:=0) id=(@x:=@x+2); empty set (0.00 sec) 

what explain this?

sql fiddle

i'm not sure why confused. code doing specifying.

during first iteration -- assuming t read in order (which not guaranteed) -- have:

id = 1, @x = 2 

then:

id = 2, @x = 4 id = 3, @x = 6 

and on. these values never equal, no rows returned.

if want every other row, use mod():

where mod(id, 2) = 1 

variables not needed.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -