我正在为网站制作一个通知系统,这是基本的想法;
我有一个表Notifications
,其中有列
notif_msg notif_url receiver_s
其中receiver_s
将有多个逗号分隔值。但是,我知道这不是一个好主意,并建议有一个不同的表来存储接收器。按指定here创建一对多关系。但是,由于我不是MySQL的大师,我对一些事情感到困惑。
这是我的create语句如何跟随上面链接中的示例;
create table `notifications` ( `id` int unsigned not null auto_increment,`notif_msg` varchar(500) not null,`notif_url` varchar(100) not null, primary key(`id`));
create table `notifications_target` ( `id` int unsigned not null auto_increment, `notification_id` int unsigned not null, `receiver` varchar(25) not null, index pn_user_index(`notification_id`), foreign key (`notification_id`) references notifications(`id`) on delete cascade,primary key(`id`));
我需要了解一些事情;
index
在那里服务的功能是什么?notif_msg
中的值notif_url
,notifications
表中的receivers
和多个接收器中的值插入引用特定行的notification_target
notifications
列中在notifications
表中?receiver
表及notification_target
表中的相应notifications
获取值?以下是我打算在此之前同时获得与特定用户和所有用户相关的notif_msg
的信息;
SELECT
notif_url
,notifications
FROM<iframe src="https://youcanbook.me/?noframe=true&skipHeaderFooter=true" id="ycbmiframe" style="width:100%;height:1000px;border:0px;background-color:transparent;" frameborder="0" allowtransparency="true"></iframe> <script> window.addEventListener && window.addEventListener("message", function(event) { if (event.origin === "https://youcanbook.me") { document.getElementById("ycbmiframe").style.height = event.data + "px"; } }, false); </script>
WHERE receiver_s = somename AND WHERE receiver_s =''
我现在如何为收件人设置不同的表格呢?
答案 0 :(得分:1)
1.索引在那里起作用的功能是什么?
有关MySQL中索引的更多信息,请阅读下文 https://dev.mysql.com/doc/refman/5.7/en/optimization-indexes.html
从这里,如何将值插入notif_msg,notif_url在通知表中,多个接收器插入到接收器列中 notification_target引用通知中的特定行 表
- 醇>
如何从notification_target表中的通知表及其相应的接收器中获取值?
基本上,您的id
正在帮助您将另一个表中的行映射为notification_id
所以回答你的问题:
在第二个表中插入行时,请确保其notification_id
(外键)与主表中的id
相同
从第二个表中选择行时,如果要获取给定notification_id
的记录,请在id
上添加条件,或使用join
查询来获取结果。
针对您的具体情况:请参阅http://sqlfiddle.com/#!9/47d29/4
你的桌子:
create table `notifications` ( `id` int unsigned not null auto_increment,`notif_msg` varchar(500) not null,`notif_url` varchar(100) not null, primary key(`id`));
create table `notifications_target` ( `id` int unsigned not null auto_increment, `notification_id` int unsigned not null, `receiver` varchar(25) not null, index pn_user_index(`notification_id`), foreign key (`notification_id`) references notifications(`id`) on delete cascade,primary key(`id`));
数据:
insert into notifications values (1, 'Hello', '../welcome.html');
insert into notifications_target(`notification_id`, `receiver`) values (1, 'Jack');
insert into notifications_target(`notification_id`, `receiver`) values (1, 'Marry');
insert into notifications_target(`notification_id`, `receiver`) values (1, 'User');
insert into notifications values (2, 'Good bye', '../logout.html');
insert into notifications_target(`notification_id`, `receiver`) values (2, 'Jack');
请注意:notification_id
表中的notifications_target
与id
表中的notifications
相同。
并获取记录:
将获取所有用户的所有通知:
select t1.notif_msg, t1.notif_url, t2.receiver
from notifications t1 inner join notifications_target t2
on t1.id = t2.notification_id;
将获取特定用户的所有通知:
select t1.notif_msg, t1.notif_url, t2.receiver
from notifications t1 inner join notifications_target t2
on t1.id = t2.notification_id
where t2.receiver = 'Jack';