我正在尝试获得CountId等于零的结果。我厌倦了下面的代码但是没有用。有人可以请帮忙:)
SELECT table1.id,table1.name2,table1.name3,table1.name4,table1.name, COUNT(table2.id ) AS CountId
from table1
LEFT JOIN table2
ON table1.name4 = table2.name4
AND table1.name3 = table2.name3
AND table1.name2 = table2.name2
WHERE CountId = 0
答案 0 :(得分:1)
您不能在SUM
子句中使用聚合函数(例如COUNT
,WHERE
等)。
在HAVING
之后使用GROUP BY
子句。在连接多个表时,最好使用别名。
SELECT t1.id,t1.name2,t1.name3,t1.name4,t1.name, COUNT(t2.id) AS CountId
from table1 t1
LEFT JOIN table2 t2 ON t1.name4 = t2.name4
AND t1.name3 = t2.name3
AND t1.name2 = t2.name2
GROUP BY t1.id,t1.name2,t1.name3,t1.name4,t1.name
HAVING COUNT(t2.id) = 0