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

前端 未结 13 1251
轮回少年
轮回少年 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条回答
  •  -上瘾入骨i
    2020-12-24 13:12

    I haven't tested this approach as I don't have access to mysqldb right now. But, I think you should be able to get this done by using a ranking function. Since mysql doesn't have an equivalent of row_number function of Oracle I think you can do it like this:

    Select * from (
    Select 
        uc.id, 
        uc.user_id, 
        uc.friend_id 
        um.message
        um.read, 
        um.time,
        @rownum := IF(@prev_val = um.conversation_id, @rownum + 1, 1) AS rank,
        @prev_val := um.conversation_id
    From
        userconversation uc,
        usermessages um,
        (select @row_num:=1) rows,
        (select @prev_val:='') partitions
    Where 
        uc.id=um.conversation_id        
        and c.userId = 222 OR c.friendId = 222 
    
    Order By 
        um.conversation_id,um.id desc
    )t where t.rank=1
    

提交回复
热议问题