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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:08:21+00:00 2026-05-13T23:08:21+00:00

I want to list messages that received specific user from other users group by

  • 0

I want to list messages that received specific user from other users group by ID’s and ordered by last message received.

If I use this query:

SELECT MAX(id), fromid, toid, message 
  FROM pro_messages 
 WHERE toid=00003 
GROUP BY fromid

I do not get last message sent from user “fromid” to user “toid” but the first message sent. Can I do that in some other way or I need to do it with two queries or join tables?

id – message id
fromid – id of user who sent message
toid – id of user who receive message (in this case user 00003)

  • 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-13T23:08:21+00:00Added an answer on May 13, 2026 at 11:08 pm

    First, you’ll want to be careful with your toid value there. The zero-padding could cause trouble if it’s interpreted as an octal number: 00010 is 9 decimal, and toid=9 is most likely not what you’re intending.

    As for your message trouble, you’re getting normal behavior for the GROUP operator in MySQL. Consider the following simplified table:

    mysql> select * from messages;
    +------+--------+------+---------+
    | id   | fromid | toid | message |
    +------+--------+------+---------+
    |    1 |    100 |    3 | hi      | 
    |    2 |    100 |    3 | there   | 
    |    3 |    100 |    3 | how     | 
    |    4 |    101 |    3 | did     | 
    |    5 |    100 |    3 | are you |  <--the message you want, right?
    +------+--------+------+---------+
    

    When you group on fromid, the database collapses all the rows matching the grouping conditions into a single row. In this case, your query will get the following results

    mysql> select max(id), fromid, toid, message from messages where toid=3 group by fromid;
    +---------+--------+------+---------+
    | max(id) | fromid | toid | message |
    +---------+--------+------+---------+
    |       5 |    100 |    3 | hi      | 
    |       4 |    101 |    3 | did     | 
    +---------+--------+------+---------+
    

    Notice now the ‘100’ row has the max(id) of 5, but the message from id=1. This is how the GROUP BY operations work. You can’t work around this with a simple query, but if you switch to a sub-select as follows, you’ll get the proper results:

    mysql> select id, fromid, toid, message from messages where id in (select max(id) from messages where toid=3 group by fromid);
    +------+--------+------+---------+
    | id   | fromid | toid | message |
    +------+--------+------+---------+
    |    4 |    101 |    3 | did     | 
    |    5 |    100 |    3 | are you | 
    +------+--------+------+---------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to list all users with their corropsonding user class. Here are simplified
I have a list of messages that needs to be sent or received, depending
I have several message queues that have specific messages on them. I've created classes
I am starting out with Silverlight. I want to display a list of messages
I want that list, because if something horrible happens, and I'll have to reinstall
I want to list (a sorted list) all my entries from an attribute called
I want to list the IP addresses and last surfing url of my visitors.
i want to list files from dev end at tty bettwen 15 and 24...should
For a new SaaS app that I'm working on, I want the end user
I want to create a list (not implying Python List ) of error 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.