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 6691659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:46:38+00:00 2026-05-26T05:46:38+00:00

I have a messages table that is structured somewhat like this: from | to

  • 0

I have a messages table that is structured somewhat like this:

from  |  to  |  date
-----------------------------------
1     |  3   |  2011-09-23 11:51:44
3     |  1   |  2011-09-23 11:56:29
3     |  2   |  2011-10-04 10:20:01
2     |  3   |  2011-10-05 07:48:00

I want to display a messages page on my website very similar to Facebook’s messages page, which shows a list of the people with which the user has a conversation. Regardless of the depth of the conversation, the page only shows each person once, along with the most recent message in that conversation with that particular person, whether it was sent or received.

The part that stumps me is that the most recent message can be either sent or received, which means that the user’s ID number can be in either the from or to column. I’m not sure how to test against both columns the way I need to.

I’m still learning how to write more complex MySQL queries, and while I feel like this is a simple case of using OR with subqueries, I can’t seem to get it right.

SOLUTION
Turns out it wasn’t really a very simple case at all. Widor took some time to help me out with this, and the following query finally seems to do the job. It hasn’t been tested thoroughly, but so far it seems to work fine:

SELECT m.*
FROM   messages m
       JOIN (SELECT Max(x.id)      AS `id`,
                    x.userid,
                    x.partnerid,
                    Max(x.mostrecent) AS `mostrecent`
             FROM  (SELECT Max(id)     `id`,
                           `from`         AS `userid`,
                           `to`           AS `partnerid`,
                           Max(`created`) AS `mostrecent`
                    FROM   messages
                    GROUP  BY `from`,
                              `to`
                    UNION
                    SELECT Max(id)     `id`,
                           `to`           AS `userid`,
                           `from`         AS `partnerid`,
                           Max(`created`) AS `mostrecent`
                    FROM   messages
                    GROUP  BY `to`,
                              `from`) AS `x`
             GROUP  BY x.`userid`,
                       x.`partnerid`) AS `y`
         ON y.id = m.id
WHERE  y.userid = $userid
  • 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-26T05:46:39+00:00Added an answer on May 26, 2026 at 5:46 am

    RE-EDITED

    My previous answer (as some other ones here) didn’t take account of the case where you have more than 2 conversation ‘partners’, which isn’t the case in your example data but I’m sure will be in real life.

    So consider the case where you now have an extra record in the data:

    1    |     4    | 2011-10-04 08:34:12
    

    My revised query is as follows:

    SELECT userid, partnerid, max(mostRecent) from (
        SELECT [from] as [userid], [to] as [partnerid], max([date]) as [mostrecent] FROM messages GROUP BY [from], [to]
        UNION 
        SELECT [to] as [userid], [from] as [partnerid],max([date]) as [mostrecent] FROM messages GROUP BY [to], [from]
    ) [x]
    WHERE userid = ?
    GROUP BY userid, partnerid
    

    So, our inner UNION gives us a dataset containing the userid along with a partnerid, i.e. who they are chatting to. This could be created separately as a view if you wanted, to simplify the query.

    The outer SELECT then retrieves one record for every ‘partner’ that the specified userid has had a chat with, along with the most recent date.

    The Max() function achieves the most recent date, and the GROUP BY takes care of ensuring we bring back more then one record for each partner.

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

Sidebar

Related Questions

I have a structure that looks like this: <div class=project_group> <table> <tr style=display:none> <td>...</td>
well i have this messages table with sample values like these: msg_id recipient_id read
I have a MySQL query which gets including some vars like that: messages TABLE
I have a messages table which looks like this: +------------+-------------+----------+ | sender_id | created_at
i have a stack of messages in database table. i want to send these
I have a scenario that looks like this: User A is a registered user.
I have a table row that has the error message in it. <tr runat=server
I have an Oracle table which contains event log messages for an application. We
I have a simple Message table, with 2 indexes: mysql> show keys from Message;
I have this in a MySQL db: table Message sender_id int recipient_id int created

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.