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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:41:30+00:00 2026-06-05T10:41:30+00:00

I have created a messaging system for users which will allow users to send

  • 0

I have created a messaging system for users which will allow users to send message to another.

For this I have created two tables.

  conversation(conversation_id,user_id1,user_id2)
  messages(message_id,conversation_id,sender_id,receiver_id,message,created_time)

If users are talking first time, a conversation_id will be created with user_id1(who initiate chat) and user_id2(to who user_id1 is sending message)

Messages table will contain all info related to message.

Now what I want is, to create a message summary page where logged in user can view his all conversation list between other users order by created_time, group by conversation_id.

Here are table data:

conversation_id | user_id1 | user_id2
     1                100       103
     2                101       103
     3                103       102        


message_id| conversation_id| sender_id| receiver_id| message | created_time
   1             1            100       103           MSG A    2012-06-08 08:38:57
   2             1            103       100           MSG B    2012-06-08 08:39:40
   3             2            101       103           MSG C    2012-06-08 08:40:20
   4             3            102       103           MSG D    2012-06-08 08:41:10

And here is what output what I am looking for: Lets say logged in user is with id: 103

conversation_id| conversation_with | last_message | created_time
      3              102              MSG D         2012-06-08 08:41:10
      2              101              MSG C         2012-06-08 08:40:20
      1              100              MSG B         2012-06-08 08:39:40

So this output is ordering by created_time, Grouping by conversation_id and displaying id of user in conversation_with , with who userid 103 is having conversation.

Can somebody provide MySQL query I will need to get this output.

  • 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-05T10:41:32+00:00Added an answer on June 5, 2026 at 10:41 am

    This was a fun one. So, you have three main things to solve.

    1. Alternate the conversation_with column depending on where the desired user shows up amongst two columns.
    2. greatest-n-per-group to find the most recent message.
    3. least-n-per-group to find the earliest message created_time.

    The first one I solved by querying twice, alternating the user_id column, unioning the results, and then ordering the unioned results by created_time. I think I can solve this with one query and no unions, but it works for now.

    2 and 3 are a little more involved. Here’s a SQL fiddle with the query: http://sqlfiddle.com/#!2/bf2b7/1

    TL;DNR

    select * from (
        select c.conversation_id, c.user_id2 as conversation_with, m1.message as last_message, m3.created_time 
        from conversation as c
          join messages as m1 on c.conversation_id = m1.conversation_id
          left outer join messages as m2 on (c.conversation_id = m2.conversation_id 
                                             and (m1.created_time < m2.created_time OR m1.created_time = m2.created_time AND m1.message_id < m2.message_id))
          join messages as m3 on c.conversation_id = m3.conversation_id
          left outer join messages as m4 on (c.conversation_id = m4.conversation_id
                                             and (m3.created_time > m4.created_time OR m3.created_time = m4.created_time AND m3.message_id > m4.message_id))
        where user_id1 = 103 and m2.message_id is null and m4.message_id is null
      union all
        select c.conversation_id, c.user_id1 as conversation_with, m1.message as last_message, m3.created_time 
        from conversation as c
          join messages as m1 on c.conversation_id = m1.conversation_id
          left outer join messages as m2 on (c.conversation_id = m2.conversation_id 
                                             and (m1.created_time < m2.created_time OR m1.created_time = m2.created_time AND m1.message_id < m2.message_id))
          join messages as m3 on c.conversation_id = m3.conversation_id
          left outer join messages as m4 on (c.conversation_id = m4.conversation_id
                                             and (m3.created_time > m4.created_time OR m3.created_time = m4.created_time AND m3.message_id > m4.message_id))
        where user_id2 = 103 and m2.message_id is null and m4.message_id is null
                                            ) as conversations
    order by created_time desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a messaging system which allows users to send messages to eachother.
I want to implement a messaging system for my app. I have users. What
I am building a simple messaging system with Rails3 that allows users to send
I have created a bar chart which is a improvement of bar graph on
Am creating a messaging system model after facebook's messages. The message inbox is threaded,
Take facebook's private messaging system where you have to keep track of sender and
I want to make an email messaging system like Gmail. I want to have
I have three different users tables, and I would like to know what is
I am developing a solution which will utilize msmq to transmit data between two
I have a simple messaging system that up till now uses file based storage.

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.