List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:September 10 2001 9:57pm
Subject:Re: Seemingly Strange Variable Behavior
View as plain text  
At 4:35 PM -0500 9/10/01, Sheridan Saint-Michel wrote:
>I ran into this and was hoping someone on this list can explain this
>behavior to me.
>
>Lets say I have the table "table" which has id and name, and five rows of
>data.
>
>I am using the query
>select table.*,if(@count,@count:=@count+1,@count:=1) as count, from table;
>
>What I get is id, name, and five rows of 1's under count.
>
>Now If I do
>
>set @count=NULL;
>select table.*,if(@count,@count:=@count+1,@count:=1) as count, from table;
>
>the count column has 1,2,3,4,5
>
>When I do select @count immediately upon opening MySQL it returns NULL.
>
>So why do I get the expected result when I do set @count=NULL; but an entire
>column of 1's when I don't... even though @count returns NULL?

This _is_ odd.  Dunno.  But there's a simpler way to do this:

set @count=0;
select table.*, @count:=@count+1 AS count from table;

>
>Sheridan Saint-Michel
>Website Administrator
>FoxJet, an ITW Company
>www.foxjet.com



-- 
Paul DuBois, paul@stripped
Thread
Seemingly Strange Variable BehaviorSheridan Saint-Michel10 Sep
  • Re: Seemingly Strange Variable BehaviorPaul DuBois10 Sep