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

前端 未结 13 1305
轮回少年
轮回少年 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:01

    Why are you breaking up the data into conversations?

    If it were me, I would use one table called 'usermessages' with the following format:

    +----+--------+----------+-------------+------------+--------+
    | id | userto | userfrom | timecreated | timeviewed | message|
    +----+--------+----------+-------------+------------+--------+
    

    A conversation is identified by the combination of the 'userto' and 'userfrom' columns. So, when you want to select all of a conversation:

    SELECT * FROM usermessages 
    WHERE (userto = :userto OR userto = :userfrom) 
    AND (userfrom = :userfrom OR userfrom = :userto) 
    ORDER BY timecreated DESC 
    LIMIT 10
    

提交回复
热议问题