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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:07:06+00:00 2026-06-03T12:07:06+00:00

I have a messages system which doubles out as a message system and chat

  • 0

I have a messages system which doubles out as a message system and chat system, these are differentiated by a field called source.

I have the messages sections sorting and grouping properly however in the chat section it’s not grouping these accordingly, please see my SQL below:

SELECT * 
FROM messages
WHERE ( senderID = "1" OR receiverID = "1" ) 
AND source = "1" 
GROUP BY receiverID, senderID 
ORDER BY addedDate DESC LIMIT 10

So the result i am expecting is just one per match, i.e. one per conversation

however am getting two for some reason.

Thanks in advance

  • 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-03T12:07:07+00:00Added an answer on June 3, 2026 at 12:07 pm

    You are probably getting two answers per chat because you would have each person in both positions

    ChatID  Sender Receiver
    1       1      2
    2       2      1
    3       1      2
    4       2      1
    
    So, you'll have a record grouped by 
    Sender / Receiver
    1        2         and
    2        1
    

    I think what you are looking for is distinct people in the conversation where the above should be considered “ONE CHAT” regardless of the position they are in. To correct this, I would do something like…

    select 
          PreQuery.*,
          m2.Message,
          m2.SenderID,
          m2.ReceiverID
       from
          ( SELECT 
                  if( m.senderID < m.receiverID, m.senderID, m.receiverID ) as Person1,
                  if( m.senderID < m.receiverID, m.receiverID, m.senderID ) as Person2,
                  max( m.ID ) as LastMessageIDPerChat,
                  max( m.AddedDate ) as LastMessageDate
               FROM 
                  messages m
               WHERE 
                      m.Source = "1"
                  AND "1" IN ( SenderID, ReceiverID )
               GROUP BY
                  Person1, 
                  Person2
               ORDER BY 
                  m.AddedDate DESC 
               LIMIT 10 ) PreQuery
    
             JOIN Messages m2
                on PreQuery.LastMessageIDPerChat = m2.ID
    

    This will ensure that whichever ID is lower will always be in the first position, and whichever person’s ID is higher is always in second position to prevent false duplicates as described above.

    Also note… GROUP BY is typically expecting all non-group by fields to be associated with some aggregation, otherwise, it will just grab the first instance of a record found with a qualifying condition. So, if you want to include same people in conversations on different days, you’ll want to add “AddedDate” to the group by so it could have…

    Person1   Person2  on 4/20
    Person1   Person3  on 4/20
    Person3   Person4  on 4/20
    Person1   Person2  on 4/18
    Person1   Person5  on 4/17
    Person3   Person4  on 4/15
    

    To get the status of who sent last, I had to wrap the query of paired people per conversation and get the last ID of that conversation. THEN, take that and re-join back to messages on that ID (if ID is actually the primary key ID of the messages table, adjust as needed). From the join, I can get the last message for the conversation AND who the sender ID and Receiver ID were for that transaction (and any other data you want from the now alias “m2” reference).

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

Sidebar

Related Questions

I have make a messaging system in which user can send messages to each
I have a system which after getting a message - enqueues it (write to
I have created a messaging system which allows users to send messages to eachother.
In my application i have a threaded commenting system which i call status messages.
I have an action that lets the customer preview system email messages and i
I have a system that will generate messages sporadically, and I would like to
On a messaging system, line breaks have been added automatically when messages are posted
I have this error message could not load assembly 'system.data.entity, version 4.2.0.0, culture=neutral,publickeytoken=b77a5c561934e089' or
I have a system that logs errors. Selection from my errortable: SELECT message, personid,
I have a micropost/messaging system and each time a new micropost or comment/message is

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.