MySQL, unfortunately, is very mild about the contents of the GROUP BY , which leads to unreliable results if you do not include all the columns in GROUP BY . This is never recommended by SELECT * in a connection request, but we will leave it for now. What you need to do is run a subquery that receives the last message for the user by date, combined with the rest of the columns.
SELECT 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` JOIN ( SELECT user_from, MAX(date) AS date FROM mail WHERE user_to = '1' GROUP BY user_from ) most_recent ON mail.user_from = most_recent.user_from AND mail.date = most_recent.date WHERE `user_to` = '1'
Edit Updated to display all recent senders, not just one.
Michael berkowski
source share