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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:55:00+00:00 2026-06-13T08:55:00+00:00

I have a table: messages with this structure: `ID` // auto increment `SenderUserID` //

  • 0

I have a table: messages with this structure:

`ID`   //  auto increment
`SenderUserID`   // foreign key: `ID` from `User` table
`ReceiverUserID`   // foreign key: `ID` from `User` table
`Message`   
`DateCreated`   // timestamp 

I need to get distinct ReceiverUserIDs from table by this conditions:

  1. Sender user should have conversations with receiver users.
  2. Selected distinct ReceiverUserID should give us last DateCreated message.

So in the results, I’ll have user last conversations ordered by creation date DESC.

When I distinct in select query, mysql return a first record, but I need maximum DateCreated for selected `ReceiverUserID to get last message in top of results.

SELECT DISTINCT `m`.`ReceiverUserID` AS `ID` FROM `messages` `m`
WHERE (`m`.`SenderUserID` = :UserID OR `m`.`ReceiverUserID` = :UserID) 
ORDER BY `m`.`DateCreated` DESC

//:UserID means LogedInUserID

It is good to know It will be exactly like facebook messages board.


ID, SenderUserID, ReceiverUserID, Message,  DateCreated
1, 12, 11, 'Hi admin', 2012-10-24 11:07:00
2, 11, 12, 'Hi guy',  2012-10-24 11:08:00
3, 11, 13, 'Hello dear customer',  2012-10-24 11:12:00
4, 11, 13, 'Are you there?',  2012-10-24 11:13:00
5, 13, 11, 'Hiiii',  2012-10-24 11:14:00  // last conversation betwen 11 & 13

Current logedin user id: 11

SELECT `m`.`ReceiverUserID` AS `ID`, MAX(`m`.`DateCreated`) as `lastmsg` 
FROM `messages` `m` WHERE (`m`.`SenderUserID` = 11 OR `m`.`ReceiverUserID` = 11) 
GROUP BY `m`.`ReceiverUserID`

We have problem in this section:
It is not return our last conversation between 11 and 13 when receiver user answered the sender message, It will be not seen in results

Results:

ID, lastmsg
12, 2012-10-24 11:08:00
13, 2012-10-24 11:13:00  // Should be: 13, 2012-10-24 11:14:00
  • 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-06-13T08:55:01+00:00Added an answer on June 13, 2026 at 8:55 am

    As I mentioned you have to do the same with SenderUserID…

    SELECT `ID`, MAX(`lastmsg`) FROM 
    (   SELECT `m`.`ReceiverUserID` AS `ID`, MAX(`m`.`DateCreated`) as `lastmsg` FROM `messages` `m`
        WHERE `m`.`SenderUserID` = :UserID
        GROUP BY `m`.`ReceiverUserID`
        UNION
        SELECT `m`.`SenderUserID` AS `ID`, MAX(`m`.`DateCreated`) as `lastmsg` FROM `messages` `m`
        WHERE `m`.`ReceiverUserID` = :UserID
        GROUP BY `m`.`SenderUserID`
    ) as table2 GROUP BY `ID` ORDER BY `lastmsg`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this kind of table for simple chat: messages table structure +----+---------+-----------+---------+------+------+ |
I have this in a MySQL db: table Message sender_id int recipient_id int created
I have table messages | id | user_id | recepient_id | message | 1
I have a simple table maintaining messages between users. The table structure looks like
okay, i'm setting up a multi-user chat system. i have a messages table, that
I have Messages and User models with corresponding tables. The Messages table has such
I have a discussion board with the following table structure: +-------------------------------+ | TABLE: MESSAGES
I have a structure that looks like this: <div class=project_group> <table> <tr style=display:none> <td>...</td>
So, i have this table structure +----+---------+-----------+---------+------+---------------------+ | id | to_user | from_user |
I have a table with the following structure: -------------- SHOW COLUMNS FROM versions --------------

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.