[英]SQL Subquery with Exists
I have a scenario that can be described with 3 tables; 我有一个可以用3个表描述的场景; member, role and memberrole.
成员,角色和成员角色。 Memberrole is the intersection of the the other two and I want to fully populate it with an outer join (from member and role).
Memberrole是其他两个的交集,我想用一个外部联接(来自member和role)完全填充它。 That is the easy part.
那是容易的部分。
Now I want to be able to update the intersection for a new member or new role. 现在,我希望能够为新成员或新角色更新交集。
What I want to accomplish looks like this (but is incorrect syntax): 我要完成的工作看起来像这样(但语法不正确):
insert into memberrole (memberid,roleid)
select member.memberid, role.roleid
from member, role
where member.memberid, roleid not exists (select memberid, roleid
from memberrole)
...any suggestions? ...有什么建议么? Thanks
谢谢
INSERT into memberrole (memberid,roleid)
SELECT m.memberid, r.roleid
FROM member m
JOIN zrole r -- carthesian product MINUS the inner product
ON NOT EXISTS (select *
FROM memberrole mr
WHERE mr.memberid = m.memberid
AND mr.roleid = r.roleid
);
Note: role
is a reserved word. 注意:
role
是保留字。 I replaced it by zrole
. 我用
zrole
代替了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.