Mysql周与年份提供不正确的结果

时间:2016-01-15 15:09:07

标签: mysql

我有一个日期为2016-01-03的字段。并希望以YEAR-WEEK的格式获得年和周的结果。

因此它应该是2015年的53周(ISO)。当我查询它显示为2016-53 - 2016年的53周(它不存在),但应该是2015-53。

SET default_week_format=3;

SELECT CONCAT( YEAR( "2016-01-03") , '-', WEEK( "2016-01-03" ) ) AS `weekAndYear`;

他们是否有解决此类问题的方法?

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要依赖DATE_FORMAT功能:

select date_format("2016-01-03", "%x-%v") as `weekAndYear`;
2015-53

(fiddle)

来自文档:

%U  Week (00..53), where Sunday is the first day of the week; WEEK() mode 0
%u  Week (00..53), where Monday is the first day of the week; WEEK() mode 1
%V  Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
%v  Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
%X  Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x  Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v

或者,或者,解析YEARWEEK的结果,将短划线固定在耳朵和周的中间位置。