消息的多个接收者(数据库字段)

发布于 2024-11-01 04:36:31 字数 1041 浏览 0 评论 0原文

我想让用户互相发送消息, 现在,这是当前的模式

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2024-11-08 04:36:31

您可以通过创建另一个表来存储收件人来更好地做到这一点,其中每行列出邮件 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.

枫以 2024-11-08 04:36:31

在不知道项目大小的情况下,通常不鼓励将多个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文