开发者

Multiple receivers for messages (database field)

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 q开发者_高级运维uery, 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜