【问题标题】:mysql order by count - ordering by valuemysql 按计数排序 - 按值排序
【发布时间】:2016-01-08 22:28:59
【问题描述】:

我正在尝试通过对数据库中的记录进行分组来计算它们的数量。这很好用,但是当我尝试订购时,它会通过与想要的不同的方法订购计数。示例结果:

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

【问题讨论】:

    标签: mysql sql-order-by


    【解决方案1】:

    试试这个查询

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

    【讨论】:

      【解决方案2】:

      您已输入查询

      ORDER BY Question, Answer
      

      如果你想让 11 出现在 2 之后,那么你肯定想要

      ORDER BY Count
      

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2012-02-07
          • 1970-01-01
          • 2011-12-24
          • 1970-01-01
          • 2021-12-17
          • 2010-12-21
          • 1970-01-01
          • 2019-02-20
          • 1970-01-01
          相关资源
          最近更新 更多