Data: (log_time is a DATETIME type)
log_id | action | log_time | user
--------------------------------------------------
1 Processed 2011-02-28 16:38:48 1
2 Processed 2011-03-02 16:56:43 5
3 Processed 2011-03-02 17:00:17 5
4 Processed 2011-03-03 08:59:33 5
Query:
SELECT log_time, user
FROM logs
WHERE action = "Processed"
GROUP BY action
HAVING MAX(log_time)
Result:
log_time | user
--------------------------
2011-02-28 16:38:48 1
Clearly, this is not having the max log_time at all. If I change the query to…
SELECT MAX(log_time), user
FROM logs
WHERE action = "Processed"
Then I get, naturally:
log_time | user
--------------------------
2011-03-03 08:59:33 1
Now, the data I obviously want is the data in row 4: March 3, but user 5. I understand that I can get this by doing a simple SELECT ... ORDER BY log_time DESC LIMIT 1. But my question is, what am I doing with these MAX() queries that isn’t correct? It would seem to me that if I ran a query with a HAVING MAX() that it would give me the row that, well, had the max. What am I not understanding about how MAX() works?
Edit: To elaborate my question, basically, when I see a query…
SELECT * FROM logs WHERE action = "Processed"
GROUP BY action HAVING MAX(log_time)
… my assumption, based on how the code appears is that it will retrieve the row with the largest log_time where action is Processed. This appears to be a faulty assumption. What, then, does HAVING MAX() even mean?
Based on the input of others, particularly Damien_The_Unbeliever, I realized that my problem was that
HAVING MAX()doesn’t actually do anything. It will just pipe out the date, which doesn’t work as it’s not being compared to anything.When I say
HAVING MAX(log_time), that translates toHAVING 2011-03-03 08:59:33, which doesn’t tell the SQL what it’s supposed to have, it’s just a statement, likeIF (5). I think.HAVINGcontinues to be somewhat of a mystery for me, but I think this is the reason why this particular issue was causing me grief.