0
select count(*),snum from enrolled group by snum where count(*)=(select count(distinct(cname)) from enrolled;);
我使用MYSQL 5.7這個語法有什麼問題?
select count(*),snum from enrolled group by snum where count(*)=(select count(distinct(cname)) from enrolled;);
我使用MYSQL 5.7這個語法有什麼問題?
在where
子句中不能使用聚合函數。你想要一個having
條款:
select count(*), snum
from enrolled
group by snum
having count(*) = (select count(distinct cname) from enrolled);
另外,只有一個分號可以出現在查詢結束。而且,雖然這不是語法錯誤,但distinct
不是函數,因此它不需要括號。
您不能在where
子句中使用總計術語。相反,您應該使用having
子句:
SELECT COUNT(*), snum
FROM enrolled
GROUP BY snum
HAVING COUNT(*) = COUNT(DISTINCT(cname))