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?
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
Post a Comment