select last message of conversation for each conversation in mysql

前端 未结 4 737
猫巷女王i
猫巷女王i 2021-01-20 03:10

my query:

SELECT *
  FROM messages_status mst
       INNER JOIN messages msg 
             ON mst.msg_id=msg.id
 WHERE mst.user_id = 1 
       AND mst.status         


        
4条回答
  •  逝去的感伤
    2021-01-20 03:50

    As you asked get the last message of each conversation You may try this

    select *
    from messages msg
    inner join
    (
      select max(created_at) created_at
      from messages
      group by conv_id
    ) m2
      on msg.created_at = m2.created_at
    
    Inner JOIN messages_status mst ON msg.id=mst.msg_id 
    WHERE mst.user_id = 1 AND mst.status NOT IN (0,3)
    

    http://sqlfiddle.com/#!2/be010/89

提交回复
热议问题