Seemingly Strange Variable Behavior

2001-09-10 Thread Sheridan Saint-Michel

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?

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


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: Seemingly Strange Variable Behavior

2001-09-10 Thread Paul DuBois

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, [EMAIL PROTECTED]

-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php