【问题标题】:MySQL Order by column = x, column asc?MySQL 按列排序 = x,列 asc?
【发布时间】:2013-04-25 22:14:34
【问题描述】:

我不会粘贴整个查询。看起来像这样。

SELECT *, MONTH(date_created) as month 
from table 
GROUP BY month ORDER BY month='3', month asc

因为现在是四月,我正在查询今年,我预计顺序如下 3、1、2、4。

相反,我退出 1、2、4、3。

如何更改语句的 ORDER BY 部分,以先按选定月份排序结果,然后按顺序显示一年中的其余月份?

【问题讨论】:

标签: mysql


【解决方案1】:

添加DESC

ORDER BY month = '3' DESC, month asc

month='3'是一个返回1或0的布尔表达式,所以基本上当结果为零时,它会在结果的最后一部分。

或者不使用DESC,使用<>

ORDER BY month <> '3', month asc

【讨论】:

    【解决方案2】:

    你必须在第一顺序添加DESCASC

    SELECT *, MONTH(date_created) as month 
    FROM table 
    GROUP BY month ORDER BY month='3' DESC, month ASC
    

    【讨论】:

      【解决方案3】:

      解决方案是按字段排序

      SELECT * FROM table
      GROUP BY month
      ORDER BY FIELD(month, 3,1,2,4)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-22
        • 1970-01-01
        • 2014-11-22
        • 2015-04-12
        • 2012-03-19
        相关资源
        最近更新 更多