个人消息的表结构
存储私人消息中用户之间的对话的最佳表结构是什么? 每个用户可以向许多收件人发送个人消息。 每条消息都有发件人标志:消息是否已删除 每条消息都有接收者标记:消息是否未读、已读或已删除 每条消息都可以删除(设置标记“已删除”)
PrivateMessages 的主页应如下所示:
例如,User1 将 Message1 发送给 User2 和 User3。 在私人消息页面上,我必须显示 2 条相同的消息:
- 发送消息 1 到用户 2
- 发送消息 1 到 用户 3
下一步 - 用户 2 回复消息 2,我将在同一页面上看到以下内容:
- 收到来自用户 2 的消息 2(回复消息 1)
- 发送消息 1 到user3
下一步,我回复 message3,我会看到
- 发送 Message3 到 user2
- 发送 Message1 到 user3
等等。
谁能提供一下表结构吗? 我正在使用 MySQL 5.5
主要问题。如何只获取每个对话框的最后一条未删除的消息?
UPD。
我需要在当前用户和其他用户之间查看主页对话框列表(带分页,按日期 DESC 排序)。
What is the best table structure to store dialogs between users in private messages?
Each user can send personal message to many recepients.
Each message has flag for sender: is message deleted or not
Each message has flag for receiver: is message unread, read or deleted
Each message can be deleted (set flag 'deleted')
PrivateMessages' main page should look like this:
E.g. User1 sends Message1 to User2 and User3.
On private message page I have to show 2 same messages:
- sent Message1 to user2
- sent Message1 to user3
next step - User2 replies to Message2, I'll see on the same page following:
- received Message2 from user2 (reply on Message1)
- sent Message1 to user3
next step, I answer to message3, I'll see
- sent Message3 to user2
- sent Message1 to user3
and so on.
Can anyone provide a table-structure?
I'm using MySQL 5.5
Main question. How can I get only the last non-deleted message of each dialog?
UPD.
I need to see on main page dialog list, between current user and other users (with pagination, sorted by Date DESC).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我将首先回答您的主要问题,然后展示我将用于此目的的表结构。
仅获取特定对话框的最后一条未删除消息:
可以使用简单的三表结构。
表 1 存储用户记录 - 每个用户一条记录。
表 2 存储消息记录 - 每条消息一条记录,外键与发送消息的用户相关。
表 3 存储消息与已向其发送消息的用户之间的相关性。
以下是用于创建上述表格图的 SQL:
I will answer your main question first, then show the table structure I will use for this.
To get only the last non-deleted message of a particular dialog:
A simple three table structure could be used.
Table 1 stores user records - one record per user.
Table 2 stores message record - one record per message, foreign key relates to the user that sent the message.
Table 3 stores the correlation between messages and users that have had the messages sent to them.
Here is the SQL that is used to create the above table diagram:
我过去曾使用仅包含 MessageID、ReceiverID 和 Status 的 MessageRecipient 表完成此操作。我在该表中也有FolderID,但您没有这个要求。 Message 表根本不存储有关收件人的任何信息。
它是检索用户消息的联接,但确实防止收件人之间的消息主题和正文重复。
I've done this in the past with a MessageRecipient table that simply contains the MessageID, ReceiverID, and Status. I had FolderID in that table as well, but you don't have that requirement. The Message table did not store any information about the recipient at all.
It is a join to retrieve a users messages, but does prevent duplication of the message subject and body between recipients.
根据您提供的信息,这是我的方法。
用户表是一个让步。我的只是
id
和name
。显然我们需要一个表来存储消息。我们需要知道谁
作者
编辑了它、主题
、消息
内容以及(可能)它的创建时间代码>/已发送。我们需要知道
message_recipients
是谁。从技术上讲,即使message.author
也会发送message
的副本(在大多数情况下),但它通常放在folder='Sent'
中代码>.其他人可能都在他们的folder="Inbox"
中找到了它。然后,用户可以将消息
移至其folder='Trash'
或将其完全删除。如果由于某种原因您需要在用户删除邮件后保留邮件,您可以通过使用folder.type='System'
folder='Deleted' 来实现>。如果没有,只需删除message_recipients
表中该message_recipient.user
的记录即可。这是相关信息。请参阅用于查询架构和数据的测试用例。
架构:
测试数据:
测试用例:获取每个消息的最后一条未删除消息对话框
我不完全确定这意味着什么,但我假设“在给定用户的收件箱中”和“不在系统已删除文件夹中”作为查询的一部分。
根据提供的测试数据,得出以下结果:
Here's my approach at this, based on the information you provided.
User table is a give in. Mine is just
id
andname
.We obviously need a table to store messages. We need to know who
author
ed it, thesubject
, themessage
content, and (probably) when it wascreated
/sent.We need to know who the
message_recipients
are. Technically even themessage.author
is sent a copy of themessage
(in most cases), but it is usually put in afolder='Sent'
. Everyone else probably got it in theirfolder="Inbox"
. User's could then move themessage
to theirfolder='Trash'
or delete it completely. If for some reason you need to retain messages after the user has deleted them, you could do so by making afolder='Deleted'
with afolder.type='System'
. If not, just delete the record in themessage_recipients
table for thatmessage_recipient.user
.So here is the info for that. See the test cases for querying after the schema and data.
Schema:
Test data:
Test Case: Get the last, non-deleted, message of each dialog
I'm not completely sure what this means, but I'll assume "in a given user's inbox" and "not in the System Deleted folder" as part of my query.
This gives, based on the test data provided, the following results:
如果我是数据库的架构师,我会制作这样的结构(大约)。
我在这里没有看到任何困难,但如果您有任何问题 - 请随时提出。
If I was an architector of the DB, I'd make structure like this (approx.)
I don't see anything hard here but if you'll got any questions - feel free to ask.
您在这里:
您 (ID1) 和其他人 (ID2) 之间的最后一条消息
here you are:
last message between you (ID1) and other person (ID2)