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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:06:10+00:00 2026-06-08T10:06:10+00:00

I try to make an SQL which bring me last message from each user

  • 0

I try to make an SQL which bring me last message from each user who send me.
I am using phpmyadmin.

I have two tables

users

id | username
6         6666
7         8888

messages

message_id | sender_id | receiver_id | subject | text | date
1            6           3             aaa       bbb    2012-07-22 00:14:41
2            6           3             aaa1      bbb1   2012-07-22 00:15:41
3            7           3             vvv       vvv    2012-07-22 00:19:41

so result must be (ordered by date(time))

3            7           3             vvv       vvv    2012-07-22 00:19:41
2            6           3             aaa1      bbb1   2012-07-22 00:15:41

I have made this query (I tested that my id = 3) below query working as I want but I think it is not so good (optimized) and there is more easier way to select this data

SELECT u.username, m.sender_id , m.subject, m.message
                FROM users u
                LEFT JOIN messages m ON m.sender_id = u.id OR m.message_id = (SELECT b.message_id FROM messages b WHERE b.receiver_id = 3 ORDER BY b.date DESC LIMIT 0,1)
                WHERE m.receiver_id = 3
                GROUP BY m.sender_id
                ORDER BY m.date 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-06-08T10:06:12+00:00Added an answer on June 8, 2026 at 10:06 am
    select * from messages 
    where message_id in(select max(msg.message_id) from messages msg
                        where msg.receiver_id = 3 group by sender_id )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Membership Provider for ASP.NET membership. I'm using first.last as the username, which
When using Cucumber with Capybara, I have to load test database data from SQL
I try to make a registration form. When user post the fields, i check
I try to make an Android application which communicates with a non-standard ISO15693 (NFC-V)
I need to make an excel file which would fetch data from connection to
I'm having problems using Hibernate and SQL Server 2008. When I try to save
I have data which is starting from 0 in my database. My php will
OKay, I will try to make this make sense lol. Basically I have 4
I have a table that contains many rows of SQL commands that make up
I have a method to check and make sure my SQL server is online,

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.