MySQL get latest conversation messages

后端 未结 1 1038
别那么骄傲
别那么骄傲 2020-12-22 12:39

I\'m making an app where users can chat to other users (1 on 1, NOT a group chat). I have a MySQL table that stores all the messages from every user, like:

f         


        
相关标签:
1条回答
  • 2020-12-22 13:14

    Try this

    SELECT * FROM
    messageTable
    WHERE from_id='abc123'
    AND `time` = ( SELECT MAX(`time`) FROM messageTable WHERE from_id='abc123' )
    
    UNION ALL
    
    SELECT * FROM
    messageTable
    WHERE to_id='abc123'
    AND `time` = ( SELECT MAX(`time`) FROM messageTable WHERE to_id='abc123' )
    
    0 讨论(0)
提交回复
热议问题