我有一个日期列,其格式如下:28-15
。
此格式是第一个数字表示周数,第二个数字表示年份。
我曾尝试使用 str_to_date(datecolumn, '%v-%y')
但没有取得好的结果。
它对列表进行排序,但顺序不正确。
我还尝试连接日期列以使字符串显示如下:
01-28-15
(第一个是星期几)并使用 str_to_date(datecolumn, '%w-%v-%y)
,没有运气。
我做错了什么?
最佳答案
来自 MySql docs :
You cannot use format "%X%V" to convert a year-week string to a date because the combination of a year and week does not uniquely identify a year and month if the week crosses a month boundary. To convert a year-week to a date, you should also specify the weekday:
SELECT STR_TO_DATE('200442 Monday', '%X%V %W');
对于您的情况,您需要假设工作日(例如星期一)和世纪(例如 2000 年),然后您可以通过以下方式获取日期:
SELECT DATE_ADD(STR_TO_DATE(CONCAT(datecolumn, ' ', 'Monday'), '%V-%X %W'), INTERVAL 2000 YEAR)
关于php - MYSQL 按日期排序(字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29936081/