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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:35:50+00:00 2026-06-18T15:35:50+00:00

I’m trying to group messages in conversations, so they can be read as a

  • 0

I’m trying to group messages in conversations, so they can be read as a conversation.

This is what I have right now

Table messages

id | from | to | message | timestamp  
1  | 1    | 2  | 'Hello' | 1351257766
2  | 1    | 3  | 'Hey!'  | 1351257767
3  | 2    | 1  | 'Hay!'  | 1351257768

Whereas the messages.from and messages.to fields correspond to a row in the users table.

What I want to do is: (not in the same query)

a. Get a list of the last messages of all the conversations from a user (so where either from or to is equal to a given user.id
b. Get all the messages that belong to a conversation.

SELECT
-- From-user:
    users_from.id               AS from_id,
    users_from.username         AS from_username,
    users_from.nickname         AS from_nickname,
    users_from.username_safe    AS from_username_safe,
    users_from.password         AS from_password,
    users_from.email            AS from_email,
    users_from.rank             AS from_rank,
    users_from.email            AS from_email,
    users_from.ip               AS from_ip,
    users_from.last_online      AS from_last_online,
    users_from.register_stamp   AS from_register_stamp,
    users_from.state            AS from_state,
    users_from.icon             AS from_icon,

-- To-user:
    users_to.id                 AS to_id,
    users_to.username           AS to_username,
    users_to.nickname           AS to_nickname,
    users_to.username_safe      AS to_username_safe,
    users_to.password           AS to_password,
    users_to.email              AS to_email,
    users_to.rank               AS to_rank,
    users_to.email              AS to_email,
    users_to.ip                 AS to_ip,
    users_to.last_online        AS to_last_online,
    users_to.register_stamp     AS to_register_stamp,
    users_to.state              AS to_state,
    users_to.icon               AS to_icon,

-- Messages:
    messages.id,
    messages.from,
    messages.to,
    messages.message,
    messages.timestamp


FROM messages

-- Some joins to get the userdata
INNER JOIN users AS users_to
    ON messages.to = users_to.id
INNER JOIN users AS users_from
    ON messages.from = users_from.id

-- We only want the latest messages:
RIGHT JOIN (
    SELECT
        max(messages.id) AS maxid,
        messages.from,
        messages.to
    FROM messages
    WHERE messages.to = {userid} OR messages.from = {userid}
) limiter
    ON messages.id = limiter.maxid

This is what I am using right now, but it isn’t working. It returns only one of the conversations.

I hope you guys can help me make this work!

UPDATE:
The answer that was given did not work. When user-1 sent a message to user-2, it worked. However when user-2 replied, it began a separate conversation (as a new row in the result).

  • 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-18T15:35:51+00:00Added an answer on June 18, 2026 at 3:35 pm

    Your query is close, but I think you need to group by the users as well. This is one approach

    RIGHT JOIN (
        SELECT
            max(messages.id) AS maxid,
            messages.from,
            messages.to
        FROM messages
        WHERE messages.to = {userid} OR messages.from = {userid}
        group by messages.from, messages.to
    ) limiter
    

    Your definition of conversation appears to be having the same two users. For this, use in:

    select m.*
    from messages m
    where `from` in ({userid1}, {userid2}) and
          `to` in ({userid1}, {userid2})
    

    By the way, from and to are bad column names, because from is a reserved word.

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

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from

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.