我正在使用以下查询来查找拥有3名以上员工的城市名称
SELECT M.NAME
FROM MasterCity M
INNER JOIN Employee E ON E.CityID = M.ID
GROUP BY E.CityID
HAVING count(E.CityID) >= 3;
它给我以下错误
Column 'MasterCity.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
怎么了。?提前致谢
最佳答案
变体#1-
SELECT MAX(M.Name) AS Name
FROM MasterCity M
JOIN Employee E ON E.CityID = M.ID
GROUP BY E.CityID
HAVING COUNT(E.CityID) >= 3;
变体#2-
SELECT M.Name
FROM MasterCity M
JOIN Employee E ON E.CityID = M.ID
GROUP BY E.CityID, M.Name
HAVING COUNT(E.CityID) >= 3;