MySQL中的ORDER BY可以正常使用1列而不是2

时间:2017-09-28 16:21:12

标签: mysql database

我希望按顺序显示计数和职业。我可以做一个就好了但是当我将第二列添加到ORDER BY语句时就不行了。

SELECT CONCAT("There are a total of ", COUNT(occupation), " ", occupation, "s.")
FROM occupations
GROUP BY occupation
ORDER BY COUNT(occupation) ASC, occupation ASC;

结果如下:

There are a total of 3 Doctors. 
There are a total of 4 Actors. 
There are a total of 4 Singers. 
There are a total of 7 Professors.

我尝试将它们反转,我也尝试指定表名(occupations.occupation)以避免任何歧义,但没有任何作用。我需要创建单独的变量吗?

2 个答案:

答案 0 :(得分:0)

如果占领中有任何资本和小组合,那么它将使其成为一个所有人,然后对其进行排序。肯定会解决排序问题。

SELECT CONCAT("There are a total of ", COUNT(occupation), " ", occupation, "s.")
FROM occupations
GROUP BY occupation
ORDER BY COUNT(occupation) ASC, initcap(occupation)  ASC;

答案 1 :(得分:0)

我发布的查询是正确的 - 事实证明应用程序正在考虑错误,因为职业是大写而不是lowerca