我写查询这个样子。这首先从您的查询开始,以获取需要的表单作为CTE,然后交叉连接它们到联系人以获取每个需要的组合,然后再离开实际的表单。
with NeededForms (<yourqueryhere>)
select distinct c.*
from Contact c cross join
NeededForms nf left outer join
Form F
on nf.FormTypeId = f.FormTypeId left outer join
ContactForm cf
on c.ContactId = cf.ContactId and
f.FormId = cf.FormId
where cf.FormId is null
我做这种方式,让你能回答什么形式具有非常类似的查询失踪的查询:
with NeededForms (<yourqueryhere>)
select c.*, nf.FormTypeId
from Contact c cross join
NeededForms nf left outer join
Form F
on nf.FormTypeId = f.FormTypeId left outer join
ContactForm cf
on c.ContactId = cf.ContactId and
f.FormId = cf.FormId
where cf.FormId is null
请发布您尝试的查询。 –
我很感谢大家的贡献。 ContactFormTypesRequired基本上是一个从Contact连接到ContactType连接到从m2m ContactTypeFormType到FormType的查询。如果我首先表明了这一点,那么您是否会使用ContactFormTypesRequired查找联系人缺少指定表单类型的问题,以及下面的答案。或者,还有更好的方法?请原谅我在原始问题中没有提供这些细节。 – cResults