Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4266846
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:49:01+00:00 2026-05-21T06:49:01+00:00

Data: (log_time is a DATETIME type) log_id | action | log_time | user ————————————————–

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:49:01+00:00Added an answer on May 21, 2026 at 6:49 am

    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 to HAVING 2011-03-03 08:59:33, which doesn’t tell the SQL what it’s supposed to have, it’s just a statement, like IF (5). I think. HAVING continues to be somewhat of a mystery for me, but I think this is the reason why this particular issue was causing me grief.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a query that would select data from multiple lines
I have a very simple query: SELECT cp.`id_connections` FROM `connections_page` cp WHERE cp.`time_end` IS
The conversion of a char data type to a DateTime data type resulted in
I retrieved data from the log table in my database. Then I started finding
How to fetch the data from binary log file and insert in our desired
My program is now still running to import data from a log file into
I'm trying to write a little software for logging data, if any type of
I am working on a data export from Sitecore, from the master database. It
I have a folder with 20 text files from several instrument runs. The data
I have the following code: $writer = new Zend_Log_Writer_Stream($path./../../../../data/logs/Log.log); $logger = new Zend_Log($writer); $logger->info(print_r($xm1,true));

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.