我正在尝试查询表格,但我很难搞清楚查询。
这是我的表格(简化):
Member
ID | NAME
1 | Frans
2 | Eric
3 | Stephan
4 | Kris
Evenement
ID | NAME
1 | Picknic
2 | Party
3 | Movie
Evenement
ID_EVENEMENT | ID_MEMBER
1 | Kris
1 | Stephan
1 | Eric
2 | Eric
2 | Frans
3 | Frans
3 | Stephan
好的,我想要的查询是:
我想
select
member_evenement.ID_MEMBER and member_evenement.ID_EVENEMENT
from
member_evenement
where
member.ID on member_evenement.ID_MEMBER
where
member_evenement.ID_MEMBER does not exist
for each member_evenement.ID_EVENEMENT
分开。
我正在使用sql server 2008 R2
我希望我能够很好地解释我的问题。
如果这些是我的基础表
Member
ID | NAME
1 | Frans
2 | Eric
3 | Stephan
4 | Kris
Evenement
ID | NAME
1 | Picknic
2 | Party
3 | Movie
Member_Evenement
ID_EVENEMENT | ID_MEMBER
1 | Kris
1 | Stephan
1 | Eric
2 | Eric
2 | Frans
3 | Frans
3 | Stephan
然后我的查询结果应如下所示:
Evenement
ID_EVENEMENT | ID_MEMBER | MEMBER_NAME | EVENEMENT_NAME
1 | 1 | Frans | Picknic
2 | 3 | Stephan | Party
2 | 4 | Kris | Party
3 | 2 | Eric | Movie
3 | 4 | Kris | Movie
答案 0 :(得分:1)
要返回未记录在member_evenement表中的成员和evenement的所有组合,请尝试以下操作:
select e.id id_evenement,
m.id id_member,
m.name member_name,
e.name evenement_name
from member m
cross join evenement e
left join member_evenement me
on e.id = me.id_evenement and m.id = me.id_member
where me.id_evenement is null or me.id_member is null
(这假设id_member实际上是成员的id,而不是样本数据中的名称。)
答案 1 :(得分:1)
SELECT e.ID AS ID_EVENEMENT, m.ID AS ID_MEMBER,
FROM Evenement e, Member m
EXCEPT
SELECT ID_EVENEMENT, ID_MEMBER
FROM member_evenement;
答案 2 :(得分:1)
另一种可能性,与Mark Bannister的完全相同的执行计划和onedaywhen的答案如下:
SELECT member.id AS memberid, evenement.id AS evenementid
FROM member
CROSS JOIN evenement WHERE
NOT EXISTS(
SELECT NULL AS [Empty]
FROM member_evenement
WHERE member_evenement.memberid = member.id
AND member_evenement.evenementid = evenement.id
)