聊天记录表结构设计之关于私聊消息的表设计

有如下表
消息记录
msg_record
from_id:发送者id
to_id:接收方id
msg:消息内容
msg_type:消息类型[文本图片]

用户表
user_info
id 用户id
nick 用户昵称
header 头像
sex 性别

user_to_user
私聊对象
from_id发送者id
to_id 对方id【此处是1多关系】

每次查看私聊消息列表只要
select to_id from user_to_user where from_id = id
一个人可能和多个人聊天,但是同时多个人也可能和一个人聊天
那么假如存在一条记录 {from_id:1,to_id:2}
是否要同时在表中插入一条{from_id:2,to_id:1}
否则select to_id from user_to_user where from_id = 2将查不到数据,
请问这个设计问题该如何解决?
请教高手!

阅读 8.3k
1 个回答
每次查看私聊消息列表只要
select to_id from user_to_user where from_id = id

这只是“我发出的消息”,不是“私聊消息列表”。

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