使用 MonthName 和 Year 排序

Sorting using MonthName and Year

我有一个这样的 table 结构:-

       ID       Month              Year

       1      January               2015
       2      February              2015
       3      March                 2014
       4      April                 2014
       5      May                   2014
       6      June                  2014
       7      July                  2014
       8      August                2014
       9      September             2014
      10      October               2014
      11      November              2014
      12      December              2014

I want to sort this using old year's month , year first if not came in month in current year otherwise want to sort using current year's month, current year (if months exist in current year) .

预期输出为:-

            Month              Year

           March                 2014
           April                 2014
           May                   2014
           June                  2014
           July                  2014
           August                2014
           September             2014
           October               2014
           November              2014
           December              2014
           January               2015
           February              2015

您可以使用 STR_TO_DATE 来获得一个可以根据您的需要进行排序的值。像

SELECT M.*, 
       STR_TO_DATE(CONCAT(M.Month,' ',M.Year),'%M %Y') AS 'myorder'
  FROM yourtable M
ORDER BY myorder ASC;

应该完成这项工作。

在查询中试试这个函数可能对你的情况有帮助

Select STR_TO_DATE(CONCAT(Month,'',Year),'%M %Y') AS 'myorder' from tableName Order By Cutom ASC

SELECT 月,年 TABLE_NAME 按年降序,月升序排列

认为这应该可以解决问题