本文介绍了MySQL yearweek()vs week()返回不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的查询似乎没有输出正确的结果.鉴于所有功能的模式都相同(1),"week("2018-12-31",1)不应显示1而不是53的值吗?

This simple query does not seem to be outputting the correct results. Given that the mode is the same (1) for all the functions shouldn't "week("2018-12-31", 1)" show a value of 1 rather than 53?

SELECT yearweek("2018-12-31", 1), yearweek("2019-01-02", 1),
week("2018-12-31", 1), week("2019-01-02", 1)

输出以下内容.

'201901','201901','53','1'

是否需要在某个地方设置更多默认开发人员?

Is there a default dev more that I need to set somewhere?

推荐答案

WEEK 函数说明:

Have a look at this example from WEEK function description:

mysql> SELECT YEAR('2000-01-01'), WEEK('2000-01-01',0);
        -> 2000, 0

有人可能会争辩说WEEK()应该返回52,因为给定的日期 实际上发生在1999年的第52周.WEEK()返回0而是 返回值为给定年份中的星期数".

One might argue that WEEK() should return 52 because the given date actually occurs in the 52nd week of 1999. WEEK() returns 0 instead so that the return value is "the week number in the given year."

以上似乎表明,所有具有0-53范围的模式都将返回相对于输入日期的年份的星期数(如果日期位于上一年的最后一周,则将返回0).

The above seem to suggest that all modes having 0-53 range would return the week number relative to the year of input date (it'll return 0 if the date falls in the last week of the previous year).

因此,如果一年的第一天=星期一,一年的第一周=今年有4天或更多天,则2018-12-31属于2018年的53周-和-2019年的第一周,并且mode参数确定返回值值:

So if first day of year = Monday and first week of year = having 4 or more days this year, then 2018-12-31 belongs to 53rd week of 2018 -and- 1st week of 2019, and the mode parameter determines the return value:

SELECT WEEK('2018-12-31', 1); -- 53
SELECT WEEK('2018-12-31', 3); -- 1

YEARWEEK 函数是明确的(结果包括年份),因此以上内容不适用.

The YEARWEEK function is unambiguous (the result includes the year) so above does not apply.

这篇关于MySQL yearweek()vs week()返回不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 08:54