views:

901

answers:

3

I have a DATETIME field. I would like to select all the records that have been updated in the last week. Right now I'm using this query:

SELECT SUM(mins_spent) as time_sum FROM todos WHERE 
lastUpdate >= '2008-12-22' AND lastUpdate <='2008-12-28'

But the results i get seem to be different depending on time of the day, e.g on 7 PM I might get 17 hours and on 11 PM I'l get 14 hours even though it should be increasing, not decreasing. I was thinking of changing the query to:

SELECT SUM(mins_spent) as time_sum FROM todos WHERE 
lastUpdate >= '2008-12-22 00:00:00' AND lastUpdate <='2008-12-28 00:00:00'

Would it help at all? Suggestions please..

+4  A: 

'2008-12-22' should equal '2008-12-22 00:00:00'.

Are you wanting "till end-of-day 28th?" If so, add 23:59:59 to the 2nd date.
Alternatively, you can use lastUpdate < "2008-12-29".

How are you tracking changes to an existing ToDo? INSERT and/or DELETE? Or UPDATE?

If you're DELETE'ing records upon "completion," you'll just have fewer records.

If you're UPDATE'ing, are you allowing dates to change to beyond the current week? If so, they'll be removed from your results.

To see what's happening, You may try grabbing a few aggregates on the table, mins_spent, and lastUpdate (mark down the values and run occasionally to see how they change).

SELECT count(*) AS Total
FROM todos
WHERE lastUpdate >= '2008-12-22' AND lastUpdate <= '2008-12-28 23:59:59'

SELECT min(mins_spent) AS Min, max(mins_spent) AS Max, avg(mins_spent) AS Avg
FROM todos
WHERE lastUpdate >= '2008-12-22' AND lastUpdate <= '2008-12-28 23:59:59'

SELECT min(lastUpdate) AS Min, max(lastUpdate) AS Max
FROM todos
WHERE lastUpdate >= '2008-12-22' AND lastUpdate <= '2008-12-28 23:59:59'
Jonathan Lonowski
I want it frm start of day 22nd-start of day 28th. Right now i update the records, and once a todo is finished i mark it as 'done' and its not touched again. So i'm pretty sure that its not being updated beyond the time of completion
Click Upvote
A: 

Judging from the MySQL 6.0 Reference Manual for Date and Time Types, you should be more or less OK as written. However, if it really isn't working, then you may have found a reportable bug.

A DATE value is coerced to the DATETIME type by adding the time portion as '00:00:00'. To perform the comparison by ignoring the time part of the DATETIME value instead, use the CAST() function to perform the comparison in the following way:

date_col = CAST(NOW() as DATE);

Your queries say '<= '2008-12-28'; that should be strictly less-than, not less-than-or-equal.

One very fine point would be: do the expressions you wrote get treated as DATETIME (because the LHS is a DATETIME column) or as a DATE (because the RHS is best treated as a DATE) or, indeed, does the DATETIME get converted to a string to match the RHS? It would make a difference if the LHS is converted to a DATE, because any time on the 28th would come in range, whereas you seem to want everything from the 21st to the 27th. What happens if you do explicitly fixup the types with casts?

Also, is the MySQL server running in the same time zone as you? There seemed to be an 'off-by-an-hour' difference between what you were seeing and the nominal end of day? I'm still not sure exactly how that might be (mis-)working, but it could be an otherwise unaccounted for factor.

Jonathan Leffler
A: 

I think i might have just read the stats incorrectly, if this problem occurs again I'll post here

Click Upvote