MySQL订购列表问题

时间:2010-10-11 08:57:37

标签: mysql sql-order-by

我有以下代码:

SELECT    q21coding, COUNT(q21coding) AS Count 
FROM      `tresults_acme` 
WHERE     q21 IS NOT NULL AND q21 <> '' 
GROUP BY  q21coding 
ORDER BY  Count DESC

它带回了以下内容:

q21coding                                  Count 
Difficulty in navigating/finding content     53
Positive comments                            28
Suggestions for improvement                  14
Inappropriate content/use                    13
Improve search facility                       6
Include information about staff and teams     5
Content needs updating                        4
Other                                        30

你会注意到其他是第二个下来 - 有没有办法确保其他总是在底部而不管计数大小?

谢谢,

荷马

3 个答案:

答案 0 :(得分:5)

ORDER BY IF(q21coding = 'Other', 1, 0) ASC, Count DESC

答案 1 :(得分:1)

@reko_t's answer有效,但实际上不需要使用IF()函数。

在MySQL中,您可以使用ORDER BY caluse中的任何表达式,而q21coding = 'Other'就足够了:

... ORDER BY q21coding = 'Other', Count DESC

如果为q21coding = 'Other'表达式,则1表达式将返回0,如果为false,则返回q21coding = 'Other'。这将在底部放置{{1}}行。

答案 2 :(得分:0)

是的,添加人工字段,其中“其他”为1,其他为0。 然后执行“ORDER BY dummy,Count”。