下面显示了我原来的MY SQL表格。
SELECT name, COUNT(city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;
我需要以下结果。
SAM有4条记录。但伦敦有两个城市的记录。 我需要 SAM记录中有多少个不同的城市。 我做了很多查询,但是无法创建我的SQL查询。
答案 0 :(得分:4)
将DISTINCT添加到count参数:
SELECT name, COUNT(DISTINCT city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;
答案 1 :(得分:3)
SELECT name, COUNT(DISTINCT city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;