按计数的mysql顺序 - 按值排序

时间:2015-10-11 11:07:56

标签: mysql sql-order-by

我试图通过对数据库进行分组来计算数据库中的记录数。这样可以正常工作,但是当我尝试按顺序排序时,它会按照不同的方法对计数进行排序。结果示例:

Question - Answer - Count

Q1 - A1 - 1

Q2 - A2 - 11

Q3 - A3 - 2

想要的结果:在2-9之后我想要11,而不是之前。查询只是:

SELECT Question, Answer, count(*) as `Count` GROUP BY Question, Answer ORDER BY Question, Answer

排序的另一个例子是mysql排序,如1,11,118,12,2,3,我期待增加的值,如1,2,3,11,12,118

3 个答案:

答案 0 :(得分:1)

尝试此查询

SELECT Question, Answer, count(*) as `Count`
FROM table
GROUP BY Question, Answer
ORDER BY count(*) ASC

答案 1 :(得分:0)

您已提入查询

ORDER BY Question, Answer

如果你希望11在2之后来,那么肯定你想要

ORDER BY Count

答案 2 :(得分:0)

问题似乎是我试图按字符值而不是整数值排序?我必须将答案转换为整数,然后才能正确排序。以下是有效的查询:

SELECT Question, Answer, count(*) as `Count` GROUP BY Question, Answer ORDER BY Question, CAST( answer AS SIGNED INTEGER )

在这里找到答案: Sorting varchar field numerically in MySQL