Way of declaring variables?

2006-04-03 Thread Yemi Obembe
Got the snippet from the mysql website:

select @last := last_insert_id()

i av the hunch that is to assign the variable 'last' to the
last_insert_id(). Im i right? is placing @ before a word mysql's way of
declaring variables? what's the work of the colon preceeding the equal sign?


--
http://ngBot.com | http://wap.ngBot.com
Nigeria's #1 website directory.


Re: Way of declaring variables?

2006-04-03 Thread Gabriel PREDA
First of all... yes... in MySQL variables are declared and used with @

Now for the : ... there is no particular role... they're there because
otherwise the SQL parser will be confused...

*SELECT column1, @neededValue=column2 FROM table_name LIMIT 1*
*SELECT column1, @neededValue:=column2 FROM table_name LIMIT 1*

In the first SQL the parser would test whether @neededValue is equal tot the
value in column2 instead of assigning to @neededValue the value from column2

This confusion can happen only in SELECT statements... so you are required
to add : only in SELECT statements.

In SET statements you don't need that... you can write:
*SET @last = last_insert_id()*
Or with values from outside MySQL:
*SET @iNeedThis = 'someText';*
Then use both in an insert statement:
*INSERT INTO table (lid, txt) VALUES (@last, @iNeedThis);*

Hope this cleared up things !

--
Gabriel PREDA
Senior Web Developer


On 4/3/06, Yemi Obembe [EMAIL PROTECTED] wrote:

 Got the snippet from the mysql website:

 select @last := last_insert_id()
 i av the hunch that is to assign the variable 'last' to the
 last_insert_id(). Im i right? is placing @ before a word mysql's way of
 declaring variables? what's the work of the colon preceeding the equal
 sign?