2015-07-13 25 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

相關問題