MySQL WEEK()的哪种模式符合ISO 8601

ste*_*red 44 mysql iso8601

MySQL的WEEK()功能是什么模式产生一年中ISO 8601周?参数2 WEEK()根据此图表设置模式:

+--------------------------------------------------------------------+
| Mode | First day of week | Range | Week 1 is the first week ...    |
|------+-------------------+-------+---------------------------------|
| 0    | Sunday            | 0-53  | with a Sunday in this year      |
|------+-------------------+-------+---------------------------------|
| 1    | Monday            | 0-53  | with more than 3 days this year |
|------+-------------------+-------+---------------------------------|
| 2    | Sunday            | 1-53  | with a Sunday in this year      |
|------+-------------------+-------+---------------------------------|
| 3    | Monday            | 1-53  | with more than 3 days this year |
|------+-------------------+-------+---------------------------------|
| 4    | Sunday            | 0-53  | with more than 3 days this year |
|------+-------------------+-------+---------------------------------|
| 5    | Monday            | 0-53  | with a Monday in this year      |
|------+-------------------+-------+---------------------------------|
| 6    | Sunday            | 1-53  | with more than 3 days this year |
|------+-------------------+-------+---------------------------------|
| 7    | Monday            | 1-53  | with a Monday in this year      |
+--------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

这些模式中的一种是否会提供一年中的ISO 8601周?

Mic*_*sen 67

在ISO周编号中,星期一是一周的第一天,因此仅将其缩小到奇数模式之一.

每个维基百科:

第01周有相互等同的描述:

  • 今年第一个星期四的一周(正式的ISO定义),
  • 1月4日的一周,
  • 第一周,在起始年份的大部分时间(四次或更多次),以及
  • 从12月29日至1月4日的周一开始的一周.

这些描述中的第三个与上表中的"今年超过3天"匹配,所以现在我们将其缩小到1或3.

最后,仍然来自维基百科(重点补充):

如果1月1日是星期一,星期二,星期三或星期四,则是01周.如果1月1日是星期五,星期六或星期日,则是上一年的第52周或第53周(没有第00周).

因此,范围必须是1-53,而不是0-53.这反过来意味着正确的模式是模式3.


小智 24

我知道这个问题已经过时了,但它的SEO位置很好:)所以只是为了让答案更加完整 - 当显示年份和周数时,你可以使用这个:

DATE_FORMAT(your_date_here, "%x-%v")
Run Code Online (Sandbox Code Playgroud)

它将产生"%v"(1-53)的iso周数和"%x"的正确年份数.

  • 尼斯.这非常适合报道!谢谢! (2认同)
  • 要使其[ISO-8601 兼容](https://en.wikipedia.org/wiki/ISO_8601#Week_dates),它必须采用“%xW%v”或“%xW%v”格式。 (2认同)

Lui*_*mas 5

ISO 8601的答案是模式3

SELECT week(your_date_column, 3) FROM your_table
Run Code Online (Sandbox Code Playgroud)