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

前端 未结 13 1141
轮回少年
轮回少年 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 12:50

    Extending the answer suggested by Watcher.

    You should consider dropping the "conversation" concept to simplify further.

    +----+---------+------+------------------+--------+----------+
    | id | message | read | time             | toUser | fromUser |
    +----+---------+------+------------------+--------+----------+
    | 1  |  test 1 |  0   | (some timestamp) |  3     |   4      |
    | 2  |  test 2 |  0   | (some timestamp) |  4     |   3      |
    +----+---------+------+------------------+--------+----------+
    

    List of all conversations for user 123:

    SELECT * FROM (
        SELECT id, message, toUser, fromUser   
        FROM userMessages 
        WHERE toUser = 123 OR fromUser = 123 
        ORDER BY id DESC
    ) AS internalTable 
    GROUP BY toUser, fromUser 
    

    List entire conversation between user 123 and user 456:

    SELECT * 
    FROM userMessages
    WHERE (toUser = 123 OR fromUser = 123) 
    AND (toUser = 456 OR fromUser = 456)
    ORDER BY time DESC
    

提交回复
热议问题