【问题标题】:Selecting latest message per thread为每个线程选择最新消息
【发布时间】:2016-06-30 14:40:38
【问题描述】:

我有一个包含以下列的表格(消息

message_id (pk)、thread_id、message_body、date_posted、posted_by、....

如何根据 date_posted 按降序选择每个线程的最新消息?

样本表

-------------------------------------------------
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
-------------------------------------------------

【问题讨论】:

  • 您应该发布一些示例表和预期结果。此外,您到目前为止尝试的 sql 查询。
  • @Ullas 完成添加示例
  • @CzarJohnDemafeliz 你可以使用内部选择查询然后分组

标签: mysql sql


【解决方案1】:

试试这个;)

select t1.*
from messages t1
inner join (
    select max(date_posted) as date_posted, thread_id
    from messages
    group by thread_id
) t2 on t2.thread_id = t1.thread_id and t2.date_posted = t1.date_posted
order by t1.date_posted

或者你可以使用in

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

SQLFiddle DEMO HERE

【讨论】:

  • 我使用了第二个,当我在最后添加 ORDER BY date_posted 时达到了预期的效果
  • @Ullas 是的,没注意到。:-)
【解决方案2】:

你可以这样做

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

【讨论】:

  • 首先您必须使用 threadid 和日期(在内部查询中)对其进行排序,然后您可以按它进行分组
  • 你有一个双重订单(错字)
  • 我不知道你从哪里得到的最新日期
【解决方案3】:

试试这个

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

【讨论】:

  • 这不起作用,因为mysql的执行顺序是:: WHERE子句SELECT子句GROUP BY子句HAVING子句ORDER BY子句
【解决方案4】:

只是另一个视角,根据 thread_id 和 date_posted 以降序给出行号。

查询

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 Fiddle Demo

【讨论】:

  • @10086 : 我没有group by 子句可以使用having 子句。
猜你喜欢
  • 2018-11-15
  • 2013-09-24
  • 2011-01-10
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多