Bug #3975 | Using a field and +/- for date calculation gives wrong result | ||
---|---|---|---|
Submitted: | 2 Jun 2004 22:24 | Modified: | 25 Jun 2004 19:08 |
Reporter: | Brian Moon | Email Updates: | |
Status: | Won't fix | Impact on me: | |
Category: | MySQL Server | Severity: | S2 (Serious) |
Version: | 4.0.16 | OS: | Linux (RedHat 9 Linux (2.4.20)) |
Assigned to: | Dmitry Lenev | CPU Architecture: | Any |
[2 Jun 2004 22:24]
Brian Moon
[2 Jun 2004 23:23]
Brian Moon
FYI, I installed 4.0.20 and it is still happening.
[2 Jun 2004 23:47]
Matthew Lord
I was able to repeat the problem in 4.0.20 but not in 4.1.2. The problem seems to be from not converting both values used in the comparison to the same type (timestamp) before converting again to do the > <.
[3 Jun 2004 0:01]
Matthew Lord
You can use the --new option with 4.0.16 or greater to make 4.0 behave more like 4.1. [mysqld] new Among other things the format for timestamps changed so that it's the same as datetimes and the problem does not surface.
[25 Jun 2004 19:08]
Dmitry Lenev
Hi, Brian! In non trivial cases MySQL compares datetime expressions either like integers (converting datetime '2003-01-01 00:00:00' to 20030101000000) or like strings. This approach works great in most cases. Unfortunately it does not work so well in your case. In query: select * from tsbug where ts >= (dt - interval 30 day); ts value is treated as integer and (dt - interval 30 day) as integer too. But since (dt - interval 30 day) is date value (not full blown date-time value) it converted to something like 20040503 and ts value is converted to something like 20030603152236, thus the result you get... To fix this behavior we either have to make both values to be treated as strings or add separate internal datetime type which should be used in comparisons... Both of these approaches are too intrusive to be implemented in 4.0. And 4.1 takes first approach... But I think in the long run we will actually take 2nd approach. So you probably should stick to 4.1 or use some workaround like: select * from tsbug where ts >= (dt - interval 30 day)*1000000; Thank you for raising up this issue!