Bug #32471 | Conditional update statement not working as expected | ||
---|---|---|---|
Submitted: | 17 Nov 2007 16:01 | Modified: | 29 Nov 2007 15:37 |
Reporter: | Tomas ValinĨius | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: General | Severity: | S3 (Non-critical) |
Version: | 5.0.45, 4.1, 5.0, 5.1 BK | OS: | Any (Linux, Windows) |
Assigned to: | Georgi Kodinov | CPU Architecture: | Any |
Tags: | conditional, UPDATE |
[17 Nov 2007 16:01]
Tomas ValinĨius
[19 Nov 2007 8:14]
Sveta Smirnova
Thank you for the report. Verified as described. Workaround: IF(`Number` < 3, 0, `Number` - 3)
[19 Nov 2007 8:15]
Sveta Smirnova
There is also inconsistence with user variables: set @v1 = 2; select Number, IF(test.`Number` - 3 < 0, 0, test.`Number` - 3), IF(@v1 - 3 < 0, 0, @v1 - 3) from test; Number IF(test.`Number` - 3 < 0, 0, test.`Number` - 3) IF(@v1 - 3 < 0, 0, @v1 - 3) 2 -1 0
[29 Nov 2007 15:37]
Georgi Kodinov
Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://dev.mysql.com/doc/ and the instructions on how to report a bug at http://bugs.mysql.com/how-to-report.php Binary operations (+, - etc) normally have a result type that is large enough to hold the result. Usually the "largest" argument type is used. Unsigned int is considered "larger" than signed. So the result of a operation between signed and unsigned int is unsigned int. This is what happens when calculating `Number` - 3 : the result is unsigned, the value wraps around (because the result of the operation is negative) and this explains the behavior. The correct way to rewrite your query is : UPDATE `test` SET `Number` = IF(`Number`< 3, 0, `Number` - 3);