消息的多个接收者(数据库字段)
我想让用户互相发送消息, 现在,这是当前的模式
CREATE TABLE IF NOT EXISTS `inbox` (
`id` int(11) NOT NULL auto_increment,
`id_usuario` int(11) NOT NULL,
`id_to` int(11) NOT NULL,
`mensaje` varchar(250) collate utf8_spanish_ci NOT NULL,
`texto` text collate utf8_spanish_ci NOT NULL,
`fecha` date NOT NULL,
`visto` int(1) NOT NULL,
`adjunto` int(1) NOT NULL,
`item` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci AUTO_INCREMENT=1 ;
对于这个问题,我认为它只是相关的:
- id_usuario - 来自用户(发送者)的唯一
- id id_to - 来自用户(接收者)的唯一
- id id - 唯一消息 id
我想知道,
如果我改变'id_to' 类型从整数(只有一个 ID)到 varchar,以便:
id_usuario - 多个唯一 ID 的逗号与用户(接收者)分隔
并存储多个接收者,例如(多个 ID):'333, 444, 555'
我能够:
- 向每个接收者显示消息吗?
仍在弄清楚查询,它会是这样的吗: “从收件箱中选择*,其中sessionid IN(idto)”
?
- 显示消息的发件人每个收件人? 仍在弄清楚查询,是否类似于“从收件箱中选择 id_to,其中 id = '254'” 然后用','爆炸?
或者,
我应该改变我的计划吗?
i want to let users send each other messages,
For now this is the current schema
CREATE TABLE IF NOT EXISTS `inbox` (
`id` int(11) NOT NULL auto_increment,
`id_usuario` int(11) NOT NULL,
`id_to` int(11) NOT NULL,
`mensaje` varchar(250) collate utf8_spanish_ci NOT NULL,
`texto` text collate utf8_spanish_ci NOT NULL,
`fecha` date NOT NULL,
`visto` int(1) NOT NULL,
`adjunto` int(1) NOT NULL,
`item` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci AUTO_INCREMENT=1 ;
For this question, i think it's only relevant:
- id_usuario - unique id from the user (sender)
- id_to - unique id from the user (reciver)
- id - unique message id
I'm wondering,
if I change 'id_to' type from integer (only one ID) to varchar, in order to:
id_usuario - multiple unique ID's comma separated from users (recivers)
and store multiple recivers like(multiple ID's): '333, 444, 555'
will I be able to:
- show each reciver the message?
Still figuring out the query, would it be something like:"select * from inbox where sesionid IN (idto)"
??
- show sender each reciver for the message?
Still figuring out the query, would it be something like"select id_to from inbox where id = '254'"
And then explode by ',' ??
Or,
Should i change my scheme?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过创建另一个表来存储收件人来更好地做到这一点,其中每行列出邮件 ID 和收件人 ID。同一消息 ID 的多个行将代表同一消息的多个收件人。
通过这种结构,您可以使用
JOIN
语句对收件人运行查询。You can do this better by creating another table to store recipients, where each row lists the message ID and a recipient ID. Multiple rows for the same message ID would represent multiple recipients for the same message.
With this kind of structure, you can run queries with recipients using
JOIN
statements.在不知道项目大小的情况下,通常不鼓励将多个 ID 存储在一列中,因为按每个 ID 进行过滤需要处理能力。
我建议创建一个名为“MessageReceivers”的表或类似的表。它将包含消息 ID 和接收消息的人的用户 ID。您将为每个收件人创建一行。
然后,通过索引,您可以通过简单的联接快速获取该用户的所有消息。
Not knowing the size of your project, storing multiple ID's in one column is usually discouraged because of the processing power required to filter by each ID.
I would suggest creating a table called "MessageReceivers" or something like that. It would contain the messageID and a userID for the person receiving the message. You would create one row per recipient.
Then with indexes, you can quickly grab all the messages for that user with a simple join.