Bug #31255 | User variables evaluated twice when used with control flow functions and join. | ||
---|---|---|---|
Submitted: | 27 Sep 2007 18:28 | Modified: | 23 Jul 2008 4:59 |
Reporter: | Benoit Heinrich | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: General | Severity: | S1 (Critical) |
Version: | ALL | OS: | Any |
Assigned to: | CPU Architecture: | Any | |
Tags: | user variables control flow functions join twice |
[27 Sep 2007 18:28]
Benoit Heinrich
[27 Sep 2007 20:33]
Benoit Heinrich
Another interesting test to complete the report. Because I'm trying to find a workaround for that kind of queries, I think I found one replacing the 'where' by a 'having': SET @count :=0; select a.ID, b.ID as bID, @count from a inner join b on a.ID=b.IDA having if(mod(bID,7) = 0 and @count < 10 and @count := @count+1, 1, 0); /* outputs: +----+-----+--------+ | ID | bID | @count | +----+-----+--------+ | 7 | 7 | 1 | | 5 | 14 | 2 | | 3 | 21 | 3 | | 1 | 28 | 4 | | 8 | 35 | 5 | | 6 | 42 | 6 | | 4 | 49 | 7 | | 2 | 56 | 8 | | 9 | 63 | 9 | | 7 | 70 | 10 | +----+-----+--------+ 10 rows in set (0.00 sec) */ Hope it will help /Benoit
[23 Jul 2008 4:59]
Valeriy Kravchuk
Thank you for a problem report. I was able to repeat your findings on recent 5.1.26-rc and 5.1.28 versions. But according to the manual, http://dev.mysql.com/doc/refman/5.1/en/user-variables.html: "The order of evaluation for user variables is undefined and may change based on the elements contained within a given query. In SELECT @a, @a := @a+1 ..., you might think that MySQL will evaluate @a first and then do an assignment second, but changing the query (for example, by adding a GROUP BY, HAVING, or ORDER BY clause) may change the order of evaluation. The general rule is never to assign a value to a user variable in one part of a statement and use the same variable in some other part of the same statement. You might get the results you expect, but this is not guaranteed." So, this is not a bug formally.