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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:43:41+00:00 2026-06-04T12:43:41+00:00

I have a table, Messages, which is set out as follows: This table stores

  • 0

I have a table, Messages, which is set out as follows:

enter image description here

This table stores all messages sent between people, however it needs to be like FaceBook’s message system.

To do this my query would need to be something like:

SELECT * FROM Messages WHERE To_User = x OR From_User = x ORDER BY Time DESC

This would fetch me every message ‘x’ either sent or received in order of time, newest ones appearing first.

However Facebook’s system keeps it sort of like a forum, so I only want one message where person y is involved in any given query result. So I don’t want this to happen:

To_User: 6
From_User: 7
Status: I agree with your message
Time: 12345

To_User: 7
To_User: 6
Status: Do you agree with my message?
Time: 12344

Say I was displaying the messages page for user 6, both messages would show up with my current system.
Instead, only the top one should be selected, because I only want to display the most recent message between user 6 and 7, rather than every one.

Naturally this would usually just ask for a LIMIT statement but I need to be able to get a lot of messages, but the only rule is that:

1- It needs to be either FROM or TO user X
2- No two messages must be selected which are FROM or TO user Y
3- The messages selected must be the NEWEST in the conversation between user X and Y

This is really hard to describe but I hope I’ve described it in enough detail for a solution.

I’ve considered doing GROUP BY but I couldn’t get it to take into account that it needed to find one that was either FROM or TO me rather than just only to or only from.

If the question isn’t described in enough detail, please tell me before down-voting and I’ll do my best to make it a bit more detailed, but I can’t think of words to describe what needs to be displayed clearly enough.

Edit:

Here’s a big example of the desired feature:

Say I sent a message to John Doe, and went on my messages page straight after, at the very top it’ll say

“To John Doe”
“The message I sent”

However if John Doe replies, that entry will not display on the page anymore, it’ll be replaced by the newest entry in the conversation between me and John Doe, so while the actual entry is still in the database, it isn’t shown anymore:

“From John Doe”
“Your message is interesting”

But if I sent one to Jane Doe afterwards, it’d push me and John Doe’s conversation so it looks like this:

“To Jane Doe”
“Hello there”

“From John Doe”
“Your message is interesting”

Now with the first query I stated in this post, the result would be:

“To Jane Doe”
“Hello there”

“From John Doe”
“Your message is interesting”

“To John Doe”
“The Message I sent”

However I don’t want to display more than one which is between me and John Doe, but since I’m looking at two different columns in the table (From and two) to dictate whether both I and John Doe are related, I don’t think a regular GROUP BY statement would work.

Now, if in addition, Bob Joe sent me a message, it might look like:

“From Bob Joe”
“Hello”

“To Jane Doe”
“Hello there”

“From John Doe”
“Your message is interesting”

  • 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-04T12:43:42+00:00Added an answer on June 4, 2026 at 12:43 pm

    You can self-join the table and use an outer join, so that any “earlier” messages will “filter out” any “non-earlier” messages.

    SELECT * FROM Messages main LEFT JOIN Messages earlier
        ON earlier.time < main.time AND 
            (
                earlier.To_User = main.To_User AND earlier.From_User = main.From_User OR  
                earlier.To_User = main.From_User AND earlier.From_User = main.To_User
            )
    WHERE (main.To_User = x OR main.From_User = x) AND earlier.id IS NULL
    ORDER BY main.Time DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table set up using phpMyAdmin which you can view in
I have an Oracle table which contains event log messages for an application. We
I have a 9 table db set up like this. TableA acct_id TableB tabB_id
i have a stack of messages in database table. i want to send these
I have this in a MySQL db: table Message sender_id int recipient_id int created
Ok I have a messages table: +-----+-----------+--------------+--------------+-----------+--------+--------------+--------------+ | id | sender_id | recipient_id |
I have tried to perform update on table which was trigger by update on
I have a large mysql table (about 5M rows) on which i frequently insert
I have a column in my database's messages table. It is called status and
I'm currently building a Windows Service which needs to process a queue of messages

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.