Bug #61408 | incorrect behaviour of user-defined variables | ||
---|---|---|---|
Submitted: | 4 Jun 2011 18:37 | Modified: | 5 Jun 2011 17:06 |
Reporter: | Daniil Kamenskiy | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: Data Types | Severity: | S3 (Non-critical) |
Version: | 5.0.90 | OS: | Any |
Assigned to: | CPU Architecture: | Any | |
Tags: | User-Defined Variables |
[4 Jun 2011 18:37]
Daniil Kamenskiy
[4 Jun 2011 19:04]
Peter Laursen
but .. DROP TABLE IF EXISTS `t`; CREATE TABLE `t` ( `a` INT(11) DEFAULT NULL, `b` INT(11) DEFAULT NULL, `c` INT(11) DEFAULT NULL, `d` INT(11) DEFAULT NULL ) ENGINE=MYISAM; INSERT INTO t VALUES(10,8,7,9); SELECT @t1:=IF(a>b,a,b), @t2:=IF(c>d,c,d) FROM t; SELECT @t3:=IF(@t2>@t1,@t2,@t1) FROM t; -- returns 10. With your example you try to assign a value to @t3 from yet non-populated @t1 and @t2. With assigning 1+2+3 in *one* statement there is no guarantee in what order they are populated (not necessarily from left to right). I am not perfectly sure but I think it is a documented limitation, actually. Peter (not a MySQL person)
[5 Jun 2011 17:06]
Valeriy Kravchuk
I think our manual (http://dev.mysql.com/doc/refman/5.0/en/user-variables.html) clearly explains that you may get unexpected results with this kind of statements: "As a general rule, you should never assign a value to a user variable and read the value within the same statement. You might get the results you expect, but this is not guaranteed. The order of evaluation for expressions involving user variables is undefined and may change based on the elements contained within a given statement. In SELECT @a, @a:=@a+1, ..., you might think that MySQL will evaluate @a first and then do an assignment second. However, changing the statement (for example, by adding a GROUP BY, HAVING, or ORDER BY clause) may cause MySQL to select an execution plan with a different order of evaluation. Another issue with assigning a value to a variable and reading the value within the same statement is that the default result type of a variable is based on its type at the start of the statement."