我有以下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” , ETC)。现在不再可以更改存储在列中的数据类型。如何使用 ORDER BY
对这些数据进行排序?
最佳答案
向您的排序依据添加一个 case
表达式,您可以在其中返回每个月的数字:
order by case `month` when 'Jan' then 1
when 'Feb' then 2
when 'Mar' then 3
...
when 'Dec' then 12
end
关于mysql - 根据字符串类型的月份对 SQL 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36696390/