MySQL week()

本文详细介绍了MySQL中WEEK函数的用法,包括如何通过指定不同的mode参数来确定周数的计算方式,以及如何根据ISO8601:1988标准进行周数计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MySQL WEEK函数介绍

通常,一年的正常年份为365天,闰年为366天。一年又可以分为许多周,每周有7天。 所以一年,我们经常有365/7 = 52周,周范围是从152

要查看给定日期属于哪个周数,您可以使用WEEK函数,如下所示:

WEEK(date, mode);

SQL

WEEK函数接受两个参数:

  • date是要获取周数的日期。
  • mode是一个可选参数,用于确定周数计算的逻辑。它允许您指定本周是从星期一还是星期日开始,返回的周数应在052之间或053之间。

如果忽略mode参数,默认情况下WEEK函数将使用default_week_format系统变量的值。

要获取default_week_format变量的当前值,请使用SHOW VARIABLES语句如下:

mysql> SHOW VARIABLES LIKE 'default_week_format';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| default_week_format | 0     |
+---------------------+-------+
1 row in set


The following table describes how the mode argument works.

ModeFirst day of weekRangeWeek 1 is the first week …
0Sunday0-53with a Sunday in this year
1Monday0-53with 4 or more days this year
2Sunday1-53with a Sunday in this year
3Monday1-53with 4 or more days this year
4Sunday0-53with 4 or more days this year
5Monday0-53with a Monday in this year
6Sunday1-53with 4 or more days this year
7Monday1-53with a Monday in this year

For mode values with a meaning of “with 4 or more days this year,” weeks are numbered according to ISO 8601:1988:

  • If the week containing January 1 has 4 or more days in the new year, it is week 1.

  • Otherwise, it is the last week of the previous year, and the next week is week 1.

WEEK函数返回一个周数,遵循ISO 8601:1988

参考文出自:1、【易百教程】作者: 初生不惑 MySQL week()函数

                     2、 MySQL DOC function_week

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值