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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:00:28+00:00 2026-05-26T01:00:28+00:00

I have some messages and a current user. Messages have an id , users

  • 0

I have some messages and a current user.

Messages have an id, users have an id.

I want to get a list of (unique) user ids where:

The first user id is for the user who sent the message to the current user with the highest message id
and all the message ids for the last user id are lower than that of any other user who sent the current user a message.

So far I am able to get a list of users who sent the current user a message with:

"SELECT sender_id AS messgr_id FROM   messages 
    WHERE  receiver_id = '$curr_id' 
UNION SELECT receiver_id AS messgr_id FROM messages
    WHERE  sender_id = '$curr_id' ";

but I am stuck as to the best way to order it in the way i want (i am learning SQL as i do this and so im no expert :))

Thanks!

  • 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-05-26T01:00:29+00:00Added an answer on May 26, 2026 at 1:00 am

    something like this should do the trick

    SELECT * 
    FROM ( 
        SELECT sender_id AS messgr_id FROM   messages 
            WHERE  receiver_id = '$curr_id' 
        UNION SELECT receiver_id AS messgr_id FROM messages
            WHERE  sender_id = '$curr_id'
    ) T1
    GROUP BY messgr_id
    ORDER BY messgr_id DESC
    

    That will give you a list of user ids with duplicates eliminated, and sorted high-to-low.


    Update:

    Re-reading your question, it sounds like you’re looking for a list of user id’s ordered by message id — which isn’t currently shown in your example. Assuming that your messages table has an id column (in addition to receiver_id and sender_id), here’s how you would do that:

    SELECT messgr_id, max(message_id) as max_message_id
    FROM ( 
        SELECT sender_id AS messgr_id, id as message_id FROM messages 
            WHERE  receiver_id = '$curr_id' 
        UNION SELECT receiver_id AS messgr_id, id as message_id FROM messages
            WHERE  sender_id = '$curr_id'
    ) T1
    GROUP BY messgr_id
    ORDER BY max_message_id DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some EDI messages (X12, HL7, etc ...) stored in an Oracle database.
I have inherited an application that pulls messages out of an MSMQ does some
We have built a web application that accepts SOAP messages, does some processing, calls
In an Activity, I have some snippet of code that fires off a Message:
I have been helping someone debug some code where the error message was Day
Is there some way when sending this message to specify that I rather have
I have a class that after it does some stuff, sends a JMS message.
I have a web service , i add some extra class which have message
Hi I have created a user message page with this plugin. The grid will
I have an android application. Based on the current geo location of user, 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.