根据字符串类型的月份对SQL数组进行排序

时间:2016-04-18 14:09:06

标签: mysql sql sorting

我有以下SELECT声明:

SELECT `facility_name`, `day`, `month`, `year`, `start_time`, `end_time`
FROM reservation_db
WHERE user_id = '$userID'
ORDER BY `facility_name` ASC, `year` ASC, `day` ASC, `start_time` ASC, `end_time` ASC"

它工作正常,但在我的表中我还有一个列month,其中包含字符串的月份(即“Ian”,“Feb”,“Mar”等) 。更改存储在列中的数据类型现在不再是一个选项..如何使用ORDER BY对它们进行排序?

1 个答案:

答案 0 :(得分:2)

在您的订单中添加case表达式,每月返回的数字为:

order by case `month` when 'Jan' then 1
                      when 'Feb' then 2
                      when 'Mar' then 3
                      ...
                      when 'Dec' then 12
         end