2015-07-13 71 views
0

我有3个表(SQL Server)。
表1. ComentListaDef(指评论)
表2. ListaDefeito(指DefectList)
表3. RespostaComentListaDef有关另一个表的SQL查询结果为空

我需要做的是给我带来一个查询(手段有关评论回答)所有“ComentListaDef.Comentarios”,“RespostaComentListaDef.IdAutor”没有回答。
我想查看与“RespostaComentListaDef.IdAutor”有关的所有“ComentListaDef.Comentarios”为空。

我试图做这个查询,但它不工作。

“ComentListaDef”是指“评论”和“RespostaComentListaDef”是指“答案”。我试图列出作者与Id =的所有评论? (RespostaComentListaDef.IdAutor)没有回答。

SELECT ComentListaDef.Comentarios, COUNT(RespostaComentListaDef.IdAutor) 
FROM ComentListaDef 
INNER JOIN ListaDefeito ON ComentListaDef.IdListaDefeitos = ListaDefeito.Id 
LEFT JOIN RespostaComentListaDef on ComentListaDef.Id = RespostaComentListaDef.IdComentListaDef 
WHERE ListaDefeito.IdRevisor = 1075 
AND ComentListaDef.IdListaDefeitos = 36 
AND RespostaComentListaDef.IdAutor = 1072 
GROUP BY ComentListaDef.Comentarios HAVING COUNT(RespostaComentListaDef.IdAutor) = 0 

回答

0

LEFT JOIN对作者的意见,只有返回的是空的项目。 (这在功能上与“不在”相同,但我认为如果您习惯于使用连接,则更清楚)

SELECT * 
FROM CommentListaDef 
LEFT JOIN RespostaComentListaDef on ComentListaDef.Id = RespostaComentListaDef.IdComentListaDef AND RespostaComentListaDef.IdAutor =1072 
WHERE RespostaComentListaDef is null 
+0

它运作良好!谢谢!!我不得不为我想要的添加更多东西,但效果很好!谢谢!!!!! – Victor

0

如果我理解正确:您希望所有没有答案的评论。

一个not exists条件应该工作:

select whatever 
from Comments c 
where not exists (select 1 from answers a where a.CommentId = c.Id 
              and a.AuthorId = @AuthorId) 
+0

正是。我希望所有的评论都没有来自作者的答案,其中Id =? (RespostaComentListaDef.IdAutor) – Victor

+0

@Victor根据我要添加的编辑添加附加条件到内部查询。 – Richard

相关问题