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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:09:07+00:00 2026-06-03T19:09:07+00:00

Say for example two users talk via direct message, you will have a conversation

  • 0

Say for example two users talk via direct message, you will have a conversation of messages .. I want to just select the latest message from that conversation and then show it as the conversation link in their message inbox … just how Facebook and Twitter messages work. They can then click that last sent message to view the whole conversation.

My messages table that contains all messages sent between users is in the following format:

sourceUserId is the id of the user who sent the message, targetUserId is the id of the user who received the message, body is the message, time is a timestamp of when the message was sent. I have kept body as abc… and time 1234 to keep this example simple, they are all different values.

+----+--------------+--------------+--------+------+
| id | sourceUserId | targetUserId |  body  | time |
+----+--------------+--------------+--------+------+
| 1  |       1      |       2      | abc... | 1234 |
| 2  |       3      |       1      | abc... | 1234 |
| 3  |       3      |       1      | abc... | 1234 |
| 4  |       1      |       3      | abc... | 1234 |
| 5  |       2      |       1      | abc... | 1234 |
| 6  |       1      |       2      | abc... | 1234 |
| 7  |       3      |       1      | abc... | 1234 |
| 8  |       4      |       1      | abc... | 1234 |
| 9  |       5      |       4      | abc... | 1234 |
| 10 |       3      |       2      | abc... | 1234 |
+----+--------------+--------------+--------+------+

To get all the messages (sent and received) for one user, I use this query:

SELECT sourceUserId, targetUserId, body,  UNIX_TIMESTAMP(time)  
FROM `usermessages`
WHERE targetUserId = 1
OR sourceUserId = 1
ORDER BY id DESC
LIMIT 10

+----+--------------+--------------+--------+------+
| id | sourceUserId | targetUserId |  body  | time |
+----+--------------+--------------+--------+------+
| 1  |       1      |       2      | abc... | 1234 |
| 2  |       3      |       1      | abc... | 1234 |
| 3  |       3      |       1      | abc... | 1234 |
| 4  |       1      |       3      | abc... | 1234 |
| 5  |       2      |       1      | abc... | 1234 |
| 6  |       1      |       2      | abc... | 1234 |
| 7  |       3      |       1      | abc... | 1234 |
| 8  |       4      |       1      | abc... | 1234 |
+----+--------------+--------------+--------+------+

but it returns multiple instances of the same message conversation and not just the latest message from a conversation between two users. Take for example rows 2,3 and 4 would all show exactly the same conversation.

I can get the query to work for just targetUserId (messages the user has received) with the following query:

SELECT sourceUserId, targetUserId, body,  UNIX_TIMESTAMP(time)  
FROM `usermessages`
WHERE targetUserId = 1
GROUP BY sourceUserId
ORDER BY id DESC
LIMIT 10

+----+--------------+--------------+--------+------+
| id | sourceUserId | targetUserId |  body  | time |
+----+--------------+--------------+--------+------+
| 2  |       3      |       1      | abc... | 1234 |
| 5  |       2      |       1      | abc... | 1234 |
| 8  |       4      |       1      | abc... | 1234 |
+----+--------------+--------------+--------+------+

and the opposite (messages sent by the user) with this, note that WHERE and GROUP BY have just been swapped:

SELECT sourceUserId, targetUserId, body,  UNIX_TIMESTAMP(time)  
FROM `usermessages`
WHERE sourceUserId = 1
GROUP BY targetUserId
ORDER BY id DESC
LIMIT 10

+----+--------------+--------------+--------+------+
| id | sourceUserId | targetUserId |  body  | time |
+----+--------------+--------------+--------+------+
| 1  |       1      |       2      | abc... | 1234 |
| 4  |       1      |       3      | abc... | 1234 |
+----+--------------+--------------+--------+------+

but if I combine the two results and group by targetUserId, sourceUserId then it does not give the correct result because all outgoing messages from 1 to (2,3,4) are grouped.

What I would like to return

I think the Pseudo Code for such a query would be:

SELECT sourceUserId, targetUserId, body,  UNIX_TIMESTAMP(time)  
FROM `usermessages`
WHERE sourceUserId = 1
OR targetUserId = 1
GROUP BY (If targetUserId != 1), (If sourceUserId != 1)
ORDER BY id DESC
LIMIT 10

+----+--------------+--------------+--------+------+
| id | sourceUserId | targetUserId |  body  | time |
+----+--------------+--------------+--------+------+
| 1  |       1      |       2      | abc... | 1234 |
| 2  |       3      |       1      | abc... | 1234 |
| 8  |       4      |       1      | abc... | 1234 |
+----+--------------+--------------+--------+------+
  • 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-03T19:09:09+00:00Added an answer on June 3, 2026 at 7:09 pm

    VERSION 2
    This is untested; it is closer but still not right… out of time though on working on this…

    SELECT Um1.sourceUserId, Um1.targetUserId, um1.body,  UNIX_TIMESTAMP(um1.time)  
    FROM usermessages um1
    WHERE um1.time = 
       (Select max(um1.time) 
        FROM usermessages um2 
        where um1.sourceUserID = um2.sourceUserID 
          and Um1.targetUserID = Um2.targetUserID, 
          and UM1.Body = UM2.body 
          and (targetuserID = 1 or sourceuserID = 1))
    Group by Um1.sourceUserId, Um1.targetUserId, um1.body
    ORDER BY id DESC
    LIMIT 10
    

    VERSION 1 (Miss)
    IMO you need to group by body as well.

    SELECT sourceUserId, targetUserId, body,  UNIX_TIMESTAMP(time)  
    FROM `usermessages`
    WHERE targetUserId = 1
    OR sourceUserId = 1
    GROUP BY sourceUserId, targetUserId, body,  UNIX_TIMESTAMP(time)
    ORDER BY id DESC
    LIMIT 10
    

    Or use distinct

    SELECT distinct sourceUserId, targetUserId, body,  UNIX_TIMESTAMP(time)  
    FROM `usermessages`
    WHERE targetUserId = 1
    OR sourceUserId = 1
    ORDER BY id DESC
    LIMIT 10
    

    I think the problem is really in that body is different or timestamp is different for those records; if not, then why are they duplicated in the table? Specifically in your output why are records 1 & 6 there… are you missing a unique index/pk which would prevent duplicates?

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

Sidebar

Related Questions

Let's say I have two or more tables filled with users and I want
Say for example, I have the following two tables: TableA { _id } TableB
I have two websites: http://example.com http://example.com/sub I have a page, let's say: http://example.com/sub/page1 Is
Let's say I have two types of users, A and B . Users of
I have two data tables in a data set for example lets say Servers:
Let's say for example i have 2 collections Comments and Users.The comments include username
I have 2 websites, lets say - example.com and example1.com example.com has a database
Say for example I have an object of var user = { Name: Dan,
say for example i have 2 div in a div like this <div style=width:
Let's say for example I have 3 servers, those 3 servers has apache installed.

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.