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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:11:51+00:00 2026-06-17T19:11:51+00:00

I’m pulling out some conversations from my database. They are being grouped by the

  • 0

I’m pulling out some conversations from my database. They are being grouped by the user_from column.

As of now, it outputs the oldest message. I want it to show the newest message.

What is the most simple way of doing this?

SELECT *
FROM (`mail`)
JOIN `users` ON `users`.`id` = `mail`.`user_from`
JOIN `users_info` ON `users_info`.`user_id` = `mail`.`user_from`
WHERE `user_to` =  '1'
GROUP BY `user_from`
ORDER BY `mail`.`date` desc

mail table

enter image description here

user table (snippet)

enter image description here

This is the current working code. The SecretAgent sent a message newer than Mail from the agency which it should be showing instead
enter image description here

  • 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-17T19:11:52+00:00Added an answer on June 17, 2026 at 7:11 pm

    MySQL is unfortunately very lenient about the contents of the GROUP BY clause, which produces unreliable results unless you include all columns in the GROUP BY. It is never recommended to SELECT * in a join query, but we’ll leave that for now. What you need to do is perform a subquery join which gets the most recent message for the user by date, joined against the rest of the columns.

    SELECT 
      /* Don't actually do this. Be explicit about columns and assign aliases where their names collide */
      users.*,
      users_info.*,
      mail.*
    FROM
      `users`
      JOIN `mail` ON `users`.`id` = `mail`.`user_from`
      JOIN `users_info` ON `users_info`.`user_id` = `mail`.`user_from`
      /* Joined subquery gets most recent message from each user */
      JOIN (
        SELECT  user_from, MAX(date) AS date
        FROM mail
        WHERE user_to = '1'
        GROUP BY user_from
        /* Joined back to the main mail table on user_from and date */
      ) most_recent ON mail.user_from = most_recent.user_from AND mail.date = most_recent.date
      WHERE `user_to` =  '1'
    

    Edit Updated to show all most recent senders rather than only one.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y’all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.