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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:28:53+00:00 2026-06-15T05:28:53+00:00

I have two tables, one that stores incoming messages and one that stores outgoing

  • 0

I have two tables, one that stores incoming messages and one that stores outgoing messages. What I would like is to be able to have a conversations view of the messages so that all incoming and outgoing messages from/to the same user id are grouped and the conversations are ordered by the most recent message (in or out)

Outgoing
----------
user_id
time
message

Incoming
----------
user_id
time
message

What I would like is to display the results such as

-> User A   9:10 pm   Nice ...
<- User A   8:45 pm   Our special is pepperoni!
-> User A   8:00 pm   What's your special dish?

<- User B   9:00 pm   We open at 5
-> User B   6:56 pm   Hello What time to you open?

<- User C   8:43 pm   Thanks!
-> User C   4:00 pm   Loved the pizza today!!

Any idea how to write a query to do this?

EDIT

If user B then texts back in, the result should be:

-> User B   9:15 pm   Ok great!
<- User B   9:00 pm   We open at 5
-> User B   6:56 pm   Hello What time to you open?

-> User A   9:10 pm   Nice ...
<- User A   8:45 pm   Our special is pepperoni!
-> User A   8:00 pm   What's your special dish?

<- User C   8:43 pm   Thanks!
-> User C   4:00 pm   Loved the pizza today!!
  • 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-15T05:28:55+00:00Added an answer on June 15, 2026 at 5:28 am

    You need to UNION the two tables and sort (ORDER BY) accordingly:

    SELECT 
        '<-' AS direction,  user_id,  time,  message
    FROM
        Outgoing
    
    UNION ALL
    
    SELECT 
        '->',               user_id,  time,  message
    FROM
        Incoming
    
    ORDER BY
        user_id ASC,
        time DESC ;
    

    After the additional explanations for the complex ordering:

    SELECT
      CASE WHEN m.d = 1 THEN '<-' ELSE '->' END AS direction, 
      m.user_id, m.time, m.message
    FROM
        ( SELECT 
            u.user_id, 
            GREATEST( COALESCE(mo.time, mi.time), 
                      COALESCE(mi.time, mo.time) ) AS maxtime
          FROM 
              ( SELECT user_id  FROM Outgoing
              UNION
                SELECT user_id  FROM Incoming 
              ) AS u
            LEFT JOIN
              ( SELECT user_id, MAX(time) AS time  FROM Outgoing  GROUP BY user_id
              ) AS mo
              ON mo.user_id = u.user_id
            LEFT JOIN
              ( SELECT user_id, MAX(time) AS time  FROM Incoming  GROUP BY user_id
              ) AS mi
              ON mi.user_id = u.user_id
        ) AS b    
      JOIN
        ( SELECT 1 AS d, user_id, time, message  FROM Outgoing
        UNION ALL
          SELECT 2 AS d, user_id, time, message  FROM Incoming
        ) AS m
        ON m.user_id = b.user_id
    ORDER BY
        b.maxtime ASC,
        m.user_id ASC,
        m.time DESC ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was thinking that I would have two tables for mysql. One for storing
I have two tables, one that stores serial number uut_build , and another for
I have two tables, one that stores user earnings and another that stores the
I have two database tables. One table stores data for a commitment that a
I have two tables: one that stores blogs and the other that stores images
I have two tables, one that stores the current price, and one that stores
I have two tables, one is 'tags' that stores tid (auto increment) and name
I have the following scenario. I have two tables. One stores multi values that
I have two tables, one that has 450 rows, the other is a Sort
I have two tables, one that has a foreign key from the other. I

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.