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

  • SEARCH
  • Home
  • 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 7160481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:24:22+00:00 2026-05-28T13:24:22+00:00

Got two versions of a query. Neither does quite what I need.. Trying to

  • 0

Got two versions of a query. Neither does quite what I need..

Trying to return all users, along with the time of the latest message sent either to OR from that user. An additional flag shows whether that last message was from the user in the current row (hence whether they’re ‘awaiting response’).

Finally, the entire result set needs to be ordered to have those users ‘awaiting response’ first, and secondarily ordered by the time of the latest message.

Version 1:

SELECT `users`.*,
MAX(`messages`.`time_sent`) AS `_message_time_sent`,
(`messages`.`user_id` = `users`.`id`) AS `_message_awaiting_response` 
FROM `users` LEFT JOIN `messages` 
ON (`messages`.`user_id` = `users`.`id` OR `messages`.`to_user_id` = `users`.`id`) 
GROUP BY `users`.`id` 
ORDER BY `_message_awaiting_response` DESC, `messages`.`time_sent` DESC

The problem with version 1: the ‘_message_sent_time’ is correct (via MAX), but the ‘_message_awaiting_response’ flag is always derived from whichever message appeared first in the join before the MAX() occurred, which may not be the most recent message.

Version 2:

SELECT DISTINCT `users`.*,
`messages`.`time_sent` AS `_message_time_sent`,
(`messages`.`user_id` = `users`.`id`) AS `_message_awaiting_response`
FROM `users` LEFT JOIN `messages`
ON (`messages`.`user_id` = `users`.`id` OR `messages`.`to_user_id` = `users`.`id`) 
GROUP BY `users`.`id` 
ORDER BY `_message_awaiting_response` DESC, `messages`.`time_sent` DESC

With version 2, the problem is largely the same – due to there being no MAX() aggregate function, the first message found in the join (not the most recent) is singled-out by DISTINCT.

Is there a simple way of making sure the latest message gets targeted here?

P.S. It needs to work in MySQL 4 😉

  • 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-28T13:24:23+00:00Added an answer on May 28, 2026 at 1:24 pm

    The simplest way to do this should be:

    SELECT * FROM
    (SELECT `users`.*,
            `messages`.`time_sent` AS `_message_time_sent`,
            (`messages`.`user_id` = `users`.`id`) AS `_message_awaiting_response` 
     FROM `users` 
     LEFT JOIN `messages` 
            ON `messages`.`user_id` = `users`.`id` OR 
               `messages`.`to_user_id` = `users`.`id`
     ORDER BY `users`.`id`, `messages`.`time_sent` DESC
    ) V
    GROUP BY V.`id` 
    ORDER BY `_message_awaiting_response` DESC, `_message_time_sent` DESC
    

    However, it should be stressed that although this probably will work, it cannot be guaranteed to work – unaggregated, ungrouped values included in a grouped select statement are normally those first in order, but MySQL documentation does not describe this behaviour as reliable.

    An alternative that should always work (provided that a given user doesn’t have multiple messages with the same timestamp) would be:

    select u.*,
           m.`time_sent` AS `_message_time_sent`,
           (m.`user_id` = u.`id`) AS `_message_awaiting_response` 
    from `users` u
    left join (select `user_id`, max(`time_sent`) AS `last_sent`
               from (select `time_sent`, `user_id` from `messages` union all
                     select `time_sent`, `to_user_id` `user_id` from `messages`) v
               group by `user_id`) lm
           on u.`user_id` = lm.`user_id`
    left join `messages` m
           ON m.`time_sent` = lm.`last_sent` and 
              lm.`user_id` in (m.`user_id`, m.`to_user_id`)
    ORDER BY `_message_awaiting_response` DESC, `_message_time_sent` DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got two versions of ruby installed, the latest using RVM. How do I
I've got two versions of my iPhone app - FULL and LITE. It's the
I need to run two versions of jQuery. I've tried aliasing j to jQuery.
Here's the situation: I've got two versions of a novel, both in txt format.
How to find the range of enums ? I have got two versions which
I've got two branches from my master: v2.1 : (version 2) I've been working
I got two views which are equally distributed on my main layout.But my two
I got two properties initialized in header file: @property (readwrite, assign) int Figure1; @property
I got two tables Product(Productname VARCHAR(10), Rating REAL) and Review(RatingID INT, Productname VARCHAR(10), Rating
I got two projects running, both written in PHP. Now I want to merge

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.