我使用以下查询来查找拥有超过 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;
关于sql - 获取3名或3名以上来自同城的员工列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20900456/