devrez
devrez

Reputation: 103

Mysql query sorting on joined tables

I have a mysql query I have a problem in sorting of the latest message of the sender here is my query below:

   SELECT 
        `Mes`.`fromid`, 
        `Mes`.`is_read`, 
        `Mes`.`id` AS `mesid`, 
        `Mes`.`message`,
        max(Mes.date) AS `date`, 
        `User`.`username`, 
        `User`.`MemberID` AS `Uid` 
   FROM `messages` AS `Mes` 
   INNER JOIN `users` AS `User` ON User.MemberID = Mes.fromid 
   WHERE (Mes.toid = 5 AND Mes.fromid <> 5) 
   GROUP BY `Mes`.`fromid` 
   ORDER BY `date` DESC

here is my table from the database: Users table

MemberID      UserName     Email             Password
1             User1       [email protected]   123456
2             User2       [email protected]   123456
3             User3       [email protected]   123456
4             User4       [email protected]   123456
5             User5       [email protected]   123456

Messages table:

id      fromid     toid    message         is_read    date  
1       5          2       hello test 1    1          2012-08-24 01:00:00
2       2          5       hello test 2    1          2012-08-24 02:00:00
3       3          5       hello test 3    1          2012-08-24 03:00:00
4       4          5       hello test 4    1          2012-08-24 04:00:00
5       2          5       hello test 5    1          2012-08-25 05:00:00   

and the ouput of my query:

SELECT 
     `Mes`.`fromid`, 
     `Mes`.`is_read`, 
     `Mes`.`id` AS `mesid`,
     `Mes`.`message`, 
     max(Mes.date) AS `date`, 
     `User`.`username`, 
     `User`.`MemberID` AS `Uid` 
FROM `messages` AS `Mes` 
INNER JOIN `users` AS `User` ON User.MemberID = Mes.fromid 
WHERE (Mes.toid = 5 AND Mes.fromid <> 5) 
GROUP BY `Mes`.`fromid` 
ORDER BY `date` DESC

is :

USERNAME     MESSAGE          DATE
user2        hello test 2     2012-08-25 05:00:00
user4        hello test 4     2012-08-25 04:00:00
user4        hello test 3     2012-08-25 03:00:00

If you have noticed Sorting from date is right, but latest message is not right. I want my output like this.

USERNAME     MESSAGE          DATE
user2        hello test 5     2012-08-25 05:00:00
user4        hello test 4     2012-08-25 04:00:00
user4        hello test 3     2012-08-25 03:00:00

Instead of the message "hello test 2" I want to sort the latest message which is "hello test 5"

Any one can help my problem?

Thank you so much...

Upvotes: 0

Views: 202

Answers (1)

John Woo
John Woo

Reputation: 263693

It should be like this,

SELECT  *                              // -- select the columns you want
FROM    Messages a
            INNER JOIN
            (
                SELECT  fromid, toid, MAX(`date`) maxDATE
                FROM    Messages
                GROUP BY fromid, toid
            ) b ON a.fromID = b.fromID AND
                   a.toid = b.toid AND
                   a.`date` = b.maxDATE
            INNER JOIN users c
                ON c.MemberID = a.fromID
WHERE   a.toid = 5 AND a.fromid <> 5
ORDER BY `date` DESC

Upvotes: 1

Related Questions