Mysql 选择每个线程的最新消息

Mysql 选择每个线程的最新消息,mysql,sql,Mysql,Sql,我有一个表(messages)包含以下列 消息id(pk),线程id,消息正文,发布日期,发布人 如何根据发布日期降序选择每个线程的最新消息 样本表 ------------------------------------------------- message_id | thread_id | body | date_posted ------------------------------------------------- 1 | 1

我有一个表(messages)包含以下列

消息id(pk),线程id,消息正文,发布日期,发布人

如何根据发布日期降序选择每个线程的最新消息

样本表

-------------------------------------------------
message_id  |  thread_id  |  body |  date_posted  
-------------------------------------------------
1           |  1          |  ...  |  2016-06-03
-------------------------------------------------
2           |  1          |  ...  |  2016-06-04
-------------------------------------------------
3           |  2          |  ...  |  2016-06-05
-------------------------------------------------
4           |  1          |  ...  |  2016-06-06
-------------------------------------------------
5           |  2          |  ...  |  2016-06-07
-------------------------------------------------
6           |  3          |  ...  |  2016-06-08
-------------------------------------------------
7           |  2          |  ...  |  2016-06-09
-------------------------------------------------
预期结果

-------------------------------------------------
message_id  |  thread_id  |  body |  date_posted  
-------------------------------------------------
7           |  2          |  ...  |  2016-06-09
-------------------------------------------------
6           |  3          |  ...  |  2016-06-08
-------------------------------------------------
4           |  1          |  ...  |  2016-06-06
-------------------------------------------------

你可以这样做

SELECT thread_id,message FROM  (Select *  from messages   ORDER BY thread_id,latestDate DESC) r group by thread_id;

你可以这样做

SELECT thread_id,message FROM  (Select *  from messages   ORDER BY thread_id,latestDate DESC) r group by thread_id;
试试这个;)

或者您可以在中使用

select *
from messages
where (date_posted, thread_id) in (
    select max(date_posted) as date_posted, thread_id
    from messages
    group by thread_id
)
order by date_posted
试试这个;)

或者您可以在
中使用

select *
from messages
where (date_posted, thread_id) in (
    select max(date_posted) as date_posted, thread_id
    from messages
    group by thread_id
)
order by date_posted
试试这个

SELECT * FROM messages GROUP BY thread_id ORDER BY  date_posted DESC;
试试这个

SELECT * FROM messages GROUP BY thread_id ORDER BY  date_posted DESC;

只是另一个角度,根据线程id和发布日期降序给出行号

查询

select t.message_id, t.thread_id, t.body, t.date_posted from( 
    select message_id, thread_id, body, date_posted, 
    (
        case thread_id when @curA 
        then @curRow := @curRow + 1 
        else @curRow := 1 and @curA := thread_id end 
    ) as rn 
    from messages m, 
    (select @curRow := 0, @curA := '') r 
    order by thread_id, date_posted desc
)t
where t.rn = 1
order by t.date_posted desc;

只是另一个角度,根据线程id和发布日期降序给出行号

查询

select t.message_id, t.thread_id, t.body, t.date_posted from( 
    select message_id, thread_id, body, date_posted, 
    (
        case thread_id when @curA 
        then @curRow := @curRow + 1 
        else @curRow := 1 and @curA := thread_id end 
    ) as rn 
    from messages m, 
    (select @curRow := 0, @curA := '') r 
    order by thread_id, date_posted desc
)t
where t.rn = 1
order by t.date_posted desc;


您应该发布一些示例表和预期结果。。此外,您目前尝试的sql查询已完成添加example@CzarJohnDemafeliz您可以使用内部选择查询获得此结果,然后按分组。您应该发布一些示例表和预期结果。。此外,您目前尝试的sql查询已完成添加example@CzarJohnDemafeliz您可以使用内部选择查询获得此结果,然后按分组。首先,您必须使用threadid和date(在内部查询中)按它排序,然后您可以按它分组。您有一个双重排序依据(键入)因此,我不知道你从哪里得到了最晚的日期。首先,你必须使用threadid和date(在内部查询中)按它排序,然后你可以按它分组。你有一个双重排序依据(打字错误)因此,我不知道您从何处获得最晚的日期。这将不起作用,因为mysql的执行顺序是::where子句SELECT子句GROUP BY子句have子句order BY子句这将不起作用,因为mysql的执行顺序是::where子句SELECT子句GROUP BY子句have子句order BY子句have子句order BY clauseI使用了第二个,我实现了我的目标当我添加发布在end@Ullas是的,没有注意到。:-)我使用了第二个,当我添加了发布在end@Ullas对没有注意到。:-@10086:我没有一个
groupby
子句可以使用
having
子句。@10086:我没有一个
groupby
子句可以使用
having
子句。