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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:53:20+00:00 2026-06-05T11:53:20+00:00

A conversation contains messages and one message can be send to 2 or more

  • 0

A conversation contains messages and one message can be send to 2 or more users.

The goal is to retrieve the last messages by user for each conversation, so where the messages in each conversation are the newest.

Also the message_user.time is when the user has read the message so when it’s equals to 0 it’s means the message has not been read yet so it’ll be even better if the first order will be the message_user where time is equal to 0 and the the message.time that are the biggest numbers.

I got those tables

Message User

user   message      time   
-----|------------|-------
 7   | 1          | 0
 8   | 1          | 0
 7   | 2          | 300
 8   | 2          | 300
 7   | 3          | 400

Message

id    text         conversation   time
---|-------------|--------------|----------
1  | blah blah1  | 1            | 200
2  | blah blah2  | 1            | 300
3  | blah blah3  | 2            | 400
4  | blah blah4  | 2            | 500

The goal is to group the messages by conversation and then get the last message_user linked to that conversation that have the bigger timestamp first.

What i tried is this but i don’t get the last messages (wrong order)

    SELECT m.user, mu.message, mu.time, mu.id, m.text, m.time as message_time, m.conversation
    FROM message_user as mu,message as m 
    WHERE mu.message=m.id AND mu.user=8
    GROUP BY m.conversation
    ORDER BY m.time DESC';

Then the next step will be:
I don’t know if it’s possible but it if the order by can ouput the message_user where the time is equal to 0 first and then second order is message.time (i don’t even know if it’s possible in one request that will be perfect!)

For the user 8 the output should be this :

text        conversation 
-----------|------------
blah blah1 | 1         //because message_user.time = 0 (means message is unread)
blah blah3 | 2         //because message.time is the highest in the conversation

Thank you !

  • 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-05T11:53:22+00:00Added an answer on June 5, 2026 at 11:53 am

    First, I’d suggest restructuring your tables to make this easier, but here’s what I think you are asking for… Each conversation, showing only the last conversion message created, with the last time and user who read it (but prefer unread over this).

    SELECT m.user as User_Sent, m.conversation, m.text, m.time as Time_Sent
    , mu.id, mu.user as User_Read, mu.time as Time_Read
    FROM message as m 
    JOIN (
      SELECT mx.conversation, MAX(mx.time) as MaxTime 
      FROM message as mx GROUP BY mx.conversation
      ) as mx
    On m.conversation = mx.conversation
    And m.time = mx.MaxTime
    JOIN (
      SELECT mu.message, MAX(mu.time) as MaxTime, MIN(mu.time) as MinTime 
      From message_user as mu 
      GROUP BY mu.message
    ) as mux
    On m.id = mux.message
    JOIN message_user as mu
    ON m.id = mu.message
    AND CASE WHEN mux.MinTime = 0 THEN mux.MinTime ELSE mux.MaxTime END = mu.time
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say for example two users talk via direct message, you will have a conversation
In my app, Users have Conversations, and Conversations have Messages. A Message belongs both
If I want to show messages in a threaded conversation, which layout should I
I've got this string which basically contains a conversation, and I want to filter
Consider the following User Control, a somewhat simplified version of my conversation control. I'm
I have created 2 table messages and Conversation. Conversation table will create an unique
I have created a messaging system for users which will allow users to send
This seemed to spark a bit of conversation on another question and I thought
I want to make a conversation view with ListBox, but I don't know how
I'm looking to build a messaging system that handles conversation much like how Facebook

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.