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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:14:09+00:00 2026-05-29T23:14:09+00:00

The following SELECT statement select * from messages where receiverID = ‘5’ group BY

  • 0

The following SELECT statement

select * 
from messages 
where receiverID = '5' 
group BY senderID 
order by id DESC

database:

id | senderID | receiverID | message
1  |  245     |    5       | test 1
2  |  89      |    5       | test 2
3  |  79      |    5       | test 3
4  |  245     |    5       | test 4
5  |  245     |    5       | test 5

For senderID=245 I expected to return the row with id=5 , but it dosent it returns row with id=1, but i want the last row. How to achieve that ?

returns:

id | senderID | receiverID | message
1  |  245     |    5       | test 1
2  |  89      |    5       | test 2
3  |  79      |    5       | test 3

Ohh I made it 😀

so this is the code that worked,for anyone with similar question

     SELECT * FROM ( SELECT * FROM messages WHERE
 receiverID = '5' ORDER BY id DESC) AS m GROUP BY senderID ORDER BY id DESC
  • 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-29T23:14:10+00:00Added an answer on May 29, 2026 at 11:14 pm

    Not sure I understand your question completely, but it sounds to me like you want:

    select max(id), 
           senderId, 
           max(receiverId), 
           max(message)
    from messages 
    where receiverID = '5' 
    group BY senderID 
    order by id DESC
    

    Note that you need to include message into your aggregate as well, otherwise you’ll get unpredicatable results (other DBMS wouldn’t allow leaving out the max(message) but MySQL will simply return a random row from the group).

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

Sidebar

Related Questions

With regards to the following statement: SELECT * FROM explorer.booking_record booking_record_ INNER JOIN explorer.client
I have the following statement SELECT id, descr from mytable which returns 1, 'Test1'
My select statement in PHP is select * from table; I use the following
consider the following pgSQL statement: SELECT DISTINCT some_field FROM some_table WHERE some_field LIKE 'text%'
I have the following SQL-statement: SELECT DISTINCT name FROM log WHERE NOT name =
I'm having trouble converting the following SQL-statement to LINQ-to-entities: SELECT l.* FROM locations l
I have tried the following two statements: SELECT col FROM db.tbl WHERE col (LIKE
I have the following SQL statement: Select Choose(1,Orders.Employee, Orders.Customer) as Name1, Choose(2,Orders.Employee, Orders.Customer) as
Let's say I have a select Statement with the following: SUM(st.tafPoints/200) as totalFriendReferrals, SUM(CASE
When I try to do the select statement, I always get the following error:

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.