SELECT DISTINCT OCCUPATION, COUNT(*)
FROM OCCUPATIONS
GROUP BY OCCUPATION
ORDER BY COUNT(*), OCCUPATION ASC;


上面的查询将为我提供独特的职业及其出现次数的升序排列。现在我要使用结果来打印语句

There are a total of [occupation_count] [occupation]s.


我试过了

SELECT  CONCAT('There are a total of ', COUNT(*), DISTINCT OCCUPATION, 's.')
FROM OCCUPATIONS
GROUP BY OCCUPATION
ORDER BY COUNT(*), OCCUPATION ASC;


但是我收到语法错误。
每个不同的职业。

我怎样才能做到这一点?我应该写一个子查询吗?

最佳答案

在使用DISTINCT的查询中几乎不需要GROUP BY,因此您的查询不需要它。因此,只需使用:

SELECT CONCAT('There are a total of ', COUNT(*), ' ', OCCUPATION, 's.')
FROM OCCUPATIONS
GROUP BY OCCUPATION
ORDER BY COUNT(*), OCCUPATION ASC;


请注意,SELECT DISTINCT是单个关键字,例如LEFT JOINIS NULL。关键字之间应该没有任何关系。

10-08 07:35