简体   繁体   中英

SELECT Last messages from last receiver. mysql

My table

id|receiver|sender|content|time
1 |   6    |  2   |  a    | 13:33
2 |   4    |  3   |  b    | 13:35
3 |   4    |  3   |  c    | 14:01
4 |   5    |  3   |  d    | 14:03
5 |   7    |  2   |  e    | 14:05
6 |   4    |  3   |  f    | 14:07

My expected result:-

id|receiver|sender|content|time
6 |   4    |  3   |  f    | 14:07
3 |   4    |  3   |  c    | 14:01
2 |   4    |  3   |  b    | 13:35

Currently my approach:-

SELECT * FROM `meassages`
  WHERE `sender` = 3 AND `receiver` IN (
    SELECT `receiver` FROM `messages` ORDER BY `time` DESC LIMIT 1
  ) ORDER BY `time` DESC

Is there an easier method?

Although your query is seemingly fine, it assumes that the last message was from sender = 3 . So, it is better to have a where in the subquery:

SELECT m.*
FROM messages m
WHERE m.sender = 3 AND 
      m.receiver = (SELECT m2.receiver
                    FROM messages m2
                    WHERE m2.sender = m.sender
                    ORDER BY m2.time DESC
                    LIMIT 1
                   )
ORDER BY m.time DESC;

Using = instead of IN emphasizes that the subquery returns one row. Also, I added table aliases and qualified column names.

Try this instead:

SELECT m1.* 
FROM `meassages` AS m1
INNER JOIN
(
   SELECT receiver, MAX(time) AS LatestTime
   FROM messages 
   group by receiver
) AS m2  ON m1.receiver = m2.receiver 
        AND m1.`time` = m2.LatestTime
WHERE m1.`sender` = 3;

The inner query, will give you the latest time for each receiver, this should be the last receiver. Then with joining the table again based on this latest time you will remove all the rows except those with the latest time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM