上次提问如何优化下面的SQL语句
SELECT A.id,A.PrjTitle,
(
SELECT COUNT(B.ID) FROM Doc_Type B WHERE
(SELECT COUNT(C.ID) FROM Doc_List C WHERE C.Doctype = B.ID AND C.PrjID=A.ID)> 0
) AS TypeCount
From Doc_Project A
得到很好的答案是:
SELECT A.id,A.PrjTitle,COUNT(B.ID)
FROM Doc_Project A,Doc_Type B
WHERE EXISTS(SELECT 1 FROM Doc_List WHERE Doctype = B.ID AND PrjID=A.ID)
GROUP BY A.id,A.PrjTitle
但是测试发现优化后的SQL语句, Doc_Project表的记录查询COUNT(B.ID) 如果为0就不显示此记录了,怎么能让查询为0也显示呢
------解决方案--------------------
left join
------解决方案--------------------
使用左外连接,left join / *=
------解决方案--------------------
try
SELECT A.id,A.PrjTitle,COUNT(B.ID)
FROM Doc_Project A left join Doc_Type B
ON EXISTS(SELECT 1 FROM Doc_List WHERE Doctype = B.ID AND PrjID=A.ID)
GROUP BY A.id,A.PrjTitle