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))