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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:55:29+00:00 2026-06-06T03:55:29+00:00

I have a simple table maintaining messages between users. The table structure looks like

  • 0

I have a simple table maintaining messages between users. The table structure looks like

     sender    receiver   message   sendtime
      1          2          m1      2012-01-01 12:12:12  
      2          1          m2      2012-01-01 12:50:20
      1          2          m3      2012-01-01 12:55:55
      1          3          m4      2012-01-02 05:05:05
      1          4          m5      2012-01-05 05:20:20
      4          1          m6      2012-01-06 06:05:00
      4          1          m7      2012-01-07 11:11:11
      2          4          m8      2012-01-08 05:01:01

Now, for the user with ID 1, I need the result like this

    sender     receiver   message   sendtime
     1           2          m3      2012-01-01 12:55:55
     1           3          m4      2012-01-02 05:05:05
     4           1          m7      2012-01-07 11:11:11

That is I need the recent messages for the particular user whether he is sender or receiver.

Though, it seem easy, I cannot find a way to write a single mysql query.

Also, I want suggestion if for this kind of solution, my table design is poor.

Note: One thing, which I think I should mention is, for instance there are multiple communication between user 1 and user 2. I need the latest communication between them two whether user 1 is a sender or receiver, only the recent.

Most users are suggesting the query, that will bring one record with the sender 1, receiver 2 and then sender 2, receiver 1. I don’t need these two record because this is a communication between same user 1 and user 2. I need one the latest one for the user specified with each of other users.

Thanks,

  • 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-06T03:55:30+00:00Added an answer on June 6, 2026 at 3:55 am
    SELECT data.* FROM 
    (SELECT MAX(sendtime) AS sendtime 
             FROM data 
             WHERE 1 IN (sender,receiver)
             GROUP BY IF (1 = sender,receiver,sender)) AS latest
    LEFT JOIN data ON latest.sendtime = data.sendtime AND 1 IN (data.sender, data.receiver)
    GROUP BY IF (1 = data.sender,data.receiver,data.sender)
    

    I recommend to use unique sequence id in your table to avoid external GROUP BY.
    If you have incrementing unique message_id (i.e. bigger message_id corresponds to later sendtime), the query would be much simpler:

    SELECT data.* FROM 
    (SELECT MAX(message_id) AS message_id 
             FROM data 
             WHERE 1 IN (sender,receiver)
             GROUP BY IF (1 = sender,receiver,sender)) AS latest
    LEFT JOIN data USING(message_id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple table (MySQL) set up for relationships between two users. [User1Id]
I have a simple table like this: CREATE TABLE `users` ( `id` int(11) unsigned
I have a simple table that has this structure: <table> <thead> <tr> <td>Some info</td>
I have a simple table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN
I have a simple table in Sybase, let's say it looks as follows: CREATE
I have a simple table structure where each item row has a status. What
I have a simple table ( Person_ ) like this: ID_ EN-EN_ FR-FR_ 1
I have a simple table: CREATE TABLE [dbo].[Users]([Surname] [nvarchar](50) COLLATE Latin1_General_CI_AI NULL) ON [PRIMARY]
i have a simple table data like: date | Jenna | Tom | Robin
I have a simple table called 'message' consisting of an 'id' column and a

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.