Is there a simpler way to achieve this style of user messaging?

前端 未结 13 1143
轮回少年
轮回少年 2020-12-24 12:20

I have created a messaging system for users, it allows them to send a message to another user. If it is the first time they have spoken then a new conversation is initiated,

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

    The simpleste way I think for this is:

    Tables:

    conversation(cid | userId | friendId | last_message_id)
    messages(mid | message | userId | read | time | cid)
    

    Then update last_message_id after each message insert by users in particulate conversation.

    And then run this simple query. It will give you what you want.

    SELECT * FROM conversation c, messages m 
    WHERE (c.userId='$uid' OR c.friendId='$uid')
    AND c.last_msg_id=m.message_id
    ORDER BY created_time DESC
    

    $uid is id of logged in user.

    So in actual what this process is doing:

    1. Displaying all conversation of logged in user.
    2. Referencing last message (so you do not need group by)
    3. And last displaying messages in order by desc.
    0 讨论(0)
提交回复
热议问题