1

我之前发布了一个问题,该问题得到了正确的回答,并导致我发现我的 YEARWEEK() 函数正在使用默认模式为零,并且给了我不想要的结果。我阅读了 YEARWEEK 的手册,其中提到了关于模式的 WEEK,它说,如果没有使用模式参数,则使用 default_week_format 值。我决定需要模式 6,因此我采取了必要的步骤将变量更改为 6。但是,只有 WEEK() 函数受此更改影响。

我得到以下结果:

SELECT YEARWEEK(NOW())
  201301
SELECT YEARWEEK(NOW(), 6)
  201302
SELECT WEEK(NOW())
  2
SELECT WEEK(NOW(), 6)
  2

这是 MySQL 的问题还是我做错了什么?我读了几次手册,它在 YEARWEEK 描述中明确指出,“模式参数的工作方式与 WEEK() 的模式参数完全一样”。但是,它并没有明确说明(就像在 WEEK 描述中所做的那样),“如果省略了 mode 参数,则使用 default_week_format 系统变量的值”。

如果没有提供变量值,YEARWEEK() 不应该使用变量的值,还是我在这方面错了?在我看来,这两个函数的行为应该是相同的,尤其是因为一个函数在其描述的这个特定部分引用另一个函数。

4

1 回答 1

1

Shouldn't YEARWEEK() use the variable's value if none is provided or am I wrong in that regard?

No.

While I understand the argument, this is documented and therefore expected behavior. From the docs on default_week_format:

The default mode value to use for the WEEK() function.

So this system variable is only used for WEEK(). However, as you noted, both functions allow week_format as a second argument. As such, I would encourage you not to set this at the server level, but in your code for clarity.

于 2013-01-09T15:53:55.213 回答