Wednesday, July 10, 2013

Mysql difference between := and = in defining user variables

In MySQL you will notice usage of := and = as an assignment operator for variables. But which one to use when? This always confused me.

So the 411 is that basically you have to use := when you assign using a select clause for e.g.
SELECT @var := some value
SELECT @last_date := IFNULL(MAX(date_observed), '2013-05-01') FROM mart_dev_console_stats


When using the SET statement you have to use = . e.g.
SET @var = some value

Another thing to note is that in stored procedures a select @var := some value will actually return a result set to the client. If you are using the variable for further processing and don't really want to send it to client I suggest using

SELECT something into @var
FROM table

See http://dev.mysql.com/doc/refman/5.0/en/select-into.html for more info

No comments:

Post a Comment