MySQL区分按对分组的最新记录

时间:2015-10-25 10:15:27

标签: mysql

我有MySQL表:

num = arr[1][2];
arr[1][2] = num;

我希望只获得最新的记录,但是按照以下方式分组:

    id  author_id  recipient_id  message        message_time
    1   16         18            lorem ipsum1    2015-10-20 14:24:49
    2   18         16            lorem ipsum2    2015-10-21 11:14:51
    3   16         22            lorem ipsum3    2015-10-21 14:42:01
    4   18         16            lorem ipsum4    2015-10-21 16:53:55
    5   16         17            lorem ipsum5    2015-10-22 10:37:44

我的预期结果:

    WHERE `author_id` = '$var' OR `recipient_id` = '$var'

我很乐意提出建议!

1 个答案:

答案 0 :(得分:0)

试试这种方式

select a.message, max(b.message_time) 
from yourTable a, yourTable b
where  a.author_id = $var OR a.recipient_id = $var
and a.id = b.id
group by a.message;