聊天表该如何设计

新手上路,请多包涵

类似csdn 私信功能

表a conversation

idsend_userto_user
1910

表b
message

idconversation_idsend_usermessage
119你好
2110你好

接收私信方如何获取会话列表 ? 这表局限太大 不知道该如何设计表

用户a 给b 发送信息时
b方如何获取a给b的会话和b给其他人的会话?

阅读 2.7k
2 个回答

如果不考虑性能和存储问题,仅从1.接收私信方如何获取会话列表,2.b方如何获取a给b的会话和b给其他人的会话这两个方面考虑的话,那么表够用了.

# 接收私信方如何获取会话列表
select * from conversation where to_user = b

# b方如何获取a给b的会话和b给其他人的会话?

(select * from conversation where to_user = b)
union
(select * from  conversation where  send_user=b)

List<ChatConversation> chatConversations = chatConversationMapper.selectList(chatConversationLambdaQueryWrapper);

    //当前通讯列表
    List<Long> chats = new ArrayList<>();
    for (ChatConversation chatConversation : chatConversations) {
        //发送方是当前用户 就取接收用户  接收方是当前用户 就取发送方用户
        Long toUser = chatConversation.getUser().equals(userId) ? chatConversation.getToUser() : chatConversation.getUser();
        chats.add(toUser);
    }

这是我写的示例 表与你conversation表相同 用程序判断就可以了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题