首页 > 数据库 >mysql周week函数

mysql周week函数

时间:2023-09-02 15:00:44浏览次数:40  
标签:week 01 函数 2024 2020 mysql yearweek

WEEK(date[,mode])

WEEK()函数会返回一个日期的周数,第2个参数mode可以指定一周是从周日开始还是周一开始,以及返回值的范围是 [0, 53] 还是 [1, 53], 如果第2个参数缺失了,则使用系统变量default_week_format的值

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 4 or more days this year
2 Sunday 1-53 with a Sunday in this year
3 Monday 1-53 with 4 or more days this year
4 Sunday 0-53 with 4 or more days this year
5 Monday 0-53 with a Monday in this year
6 Sunday 1-53 with 4 or more days this year
7 Monday 1-53 with a Monday in this year

下面这张图用来方便之后的测试
1月1号日历

模式0

由于default_week_format默认为0,所以模式0为默认模式
一周的第一天是周日,第一周是今年第一个有周日的周,第一周之前今年的日期为第0周

mysql> select week('2020-01-01', 0), week('2020-01-05', 0);
+-----------------------+-----------------------+
| week('2020-01-01', 0) | week('2020-01-05', 0) |
+-----------------------+-----------------------+
|                     0 |                     1 |
+-----------------------+-----------------------+

模式1

一周的第一天是周一,第一周是今年第一个有4天或4天以上的周,第一周之前的今年的日期为第0周

mysql> select week('2020-01-01', 1), week('2021-01-01', 1);
+-----------------------+-----------------------+
| week('2020-01-01', 1) | week('2021-01-01', 1) |
+-----------------------+-----------------------+
|                     1 |                     0 |
+-----------------------+-----------------------+

模式3

一周的第一天是周一,第一周是今年第一个有4天或4天以上的周,第一周之前的今年的日期为上一年的第52/53周,即最后一周

mysql> select week('2020-01-01', 3), week('2021-01-01', 3), week('2022-01-01', 3);
+-----------------------+-----------------------+-----------------------+
| week('2020-01-01', 3) | week('2021-01-01', 3) | week('2022-01-01', 3) |
+-----------------------+-----------------------+-----------------------+
|                     1 |                    53 |                    52 |
+-----------------------+-----------------------+-----------------------+

mysql> select yearweek('2020-01-01', 3), yearweek('2021-01-01', 3), yearweek('2022-01-01', 3);
+---------------------------+---------------------------+---------------------------+
| yearweek('2020-01-01', 3) | yearweek('2021-01-01', 3) | yearweek('2022-01-01', 3) |
+---------------------------+---------------------------+---------------------------+
|                    202001 |                    202053 |                    202152 |
+---------------------------+---------------------------+---------------------------+

模式5

一周的第一天是周一,第一周是今年第一个有星期一的周,第一周之前的今年的日期为第0周

mysql> select week('2020-01-01', 5), week('2024-01-01', 5);
+-----------------------+-----------------------+
| week('2020-01-01', 5) | week('2024-01-01', 5) |
+-----------------------+-----------------------+
|                     0 |                     1 |
+-----------------------+-----------------------+

模式7

一周的第一天是周一,第一周是今年第一个有星期一的周,第一周之前的今年的日期为上一年的第52或53周,即最后一周

mysql> select week('2019-01-01', 7), week('2020-01-01', 7), week('2024-01-01', 7);
+-----------------------+-----------------------+-----------------------+
| week('2019-01-01', 7) | week('2020-01-01', 7) | week('2024-01-01', 7) |
+-----------------------+-----------------------+-----------------------+
|                    53 |                    52 |                     1 |
+-----------------------+-----------------------+-----------------------+

mysql> select yearweek('2019-01-01', 7), yearweek('2020-01-01', 7), yearweek('2024-01-01', 7);
+---------------------------+---------------------------+---------------------------+
| yearweek('2019-01-01', 7) | yearweek('2020-01-01', 7) | yearweek('2024-01-01', 7) |
+---------------------------+---------------------------+---------------------------+
|                    201853 |                    201952 |                    202401 |
+---------------------------+---------------------------+---------------------------+

系统变量default_week_format

Integer类型,默认值为0,范围为[0, 7]

WEEKDAY(date)

返回日期的周的序号, 0:Monday, ..., 6:Sunday

mysql> select weekday('2023-01-01'), weekday('2024-01-01');
+-----------------------+-----------------------+
| weekday('2023-01-01') | weekday('2024-01-01') |
+-----------------------+-----------------------+
|                     6 |                     0 |
+-----------------------+-----------------------+

WEEKOFYEAR(date)

相当于 WEEK(date,3)

mysql> select week('2023-01-01', 3), weekofyear('2023-01-01'), week('2024-01-01', 3), weekofyear('2024-01-01');
+-----------------------+--------------------------+-----------------------+--------------------------+
| week('2023-01-01', 3) | weekofyear('2023-01-01') | week('2024-01-01', 3) | weekofyear('2024-01-01') |
+-----------------------+--------------------------+-----------------------+--------------------------+
|                    52 |                       52 |                     1 |                        1 |
+-----------------------+--------------------------+-----------------------+--------------------------+

YEARWEEK(date), YEARWEEK(date,mode)

返回日期的年和周, 其参数modeWEEK()函数中的mode的含义完全相同
WEEK()不同的是: 如果缺失参数mode时,默认值为0,不受系统变量default_week_format的影响

# `default_week_format` 默认值为0
mysql> show variables like '%default_week_format%';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| default_week_format | 0     |
+---------------------+-------+

# 在`mode`缺失和等于1的情况下, 查看 2024-01-01 的`WEEK()`和`YEARWEEK()`的结果
mysql> select week('2024-01-01'), week('2024-01-01', 7), yearweek('2024-01-01'), yearweek('2024-01-01', 7);
+--------------------+-----------------------+------------------------+---------------------------+
| week('2024-01-01') | week('2024-01-01', 7) | yearweek('2024-01-01') | yearweek('2024-01-01', 7) |
+--------------------+-----------------------+------------------------+---------------------------+
|                  0 |                     1 |                 202353 |                    202401 |
+--------------------+-----------------------+------------------------+---------------------------+

# 修改`default_week_format`的值为7
mysql> set default_week_format=7;
Query OK, 0 rows affected (0.00 sec)

# 检查`default_week_format`的值已经修改为7
mysql> show variables like '%default_week_format%';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| default_week_format | 7     |
+---------------------+-------+

# 再次查询 2024-01-01 的结果, 发现在缺失`mode`的情况下: `WEEK()`的值已经从之前的0变为1和模式7的结果相同,但`YEARWEEK()`的结果没有变化
mysql> select week('2024-01-01'), week('2024-01-01', 7), yearweek('2024-01-01'), yearweek('2024-01-01', 7);
+--------------------+-----------------------+------------------------+---------------------------+
| week('2024-01-01') | week('2024-01-01', 7) | yearweek('2024-01-01') | yearweek('2024-01-01', 7) |
+--------------------+-----------------------+------------------------+---------------------------+
|                  1 |                     1 |                 202353 |                    202401 |
+--------------------+-----------------------+------------------------+---------------------------+


注意:返回结果中的年不一定和日期中的年相同, 结果中的周也不一定和WEEK()返回的周相同

mysql> select week('2020-01-01', 0), yearweek('2020-01-01', 0);
+-----------------------+---------------------------+
| week('2020-01-01', 0) | yearweek('2020-01-01', 0) |
+-----------------------+---------------------------+
|                     0 |                    201952 |
+-----------------------+---------------------------+

参考

MySQL日期时间函数
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_week

MySQL系统变量
https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_default_week_format

标签:week,01,函数,2024,2020,mysql,yearweek
From: https://www.cnblogs.com/qode/p/17673676.html

相关文章

  • 泛微E-Office mysql_config.ini 数据库信息泄漏漏洞
    漏洞描述泛微E-Officemysql_config.ini文件可直接访问,泄漏数据库账号密码等信息漏洞复现fofa语法:app="泛微-EOffice"登录页面如下:验证POC:/mysql_config.ininuclei批量yaml文件id:EOffice_mysql_config_information_leakinfo:name:泛微OAE-Officemysql_config.i......
  • 关于windows定时任务备份mysql
    windows 定时一、右击我的电脑->选择管理->任务计划程序,打开计划任务二、开始创建任务计划。1、常规设置?都懂不再多说。2、触发器:新建->设置一个时间3、操作:新建->选择一个可执行程序,参数如果执行PHP备份mysql。首先mysql加入环境变量,直到mysql在命令行能执行如在path中新......
  • docker-compose教程:部署MySQL完整步骤
    目录结构root@localhostmysql#tree.├──conf│└──my.cnf├──db├──docker-compose.yml└──init└──init.sqldocker-compose.ymlversion:'2'services:mysql:#network_mode:"host" #将直接使用主机端口environment:......
  • 『学习笔记』狄利克雷生成函数
    定义一般地,对于一个函数\(f\),定义它的狄利克雷生成函数(简写为DGF)为:\[\tilde{F}(x)=\sum_{i\ge1}^\infty\dfrac{f_i}{i^x}.\]即:\[\tilde{F}(x)=f_1+\dfrac{f_2}{i^2}+\dfrac{f_3}{i^3}+\dfrac{f_4}{i^4}+\cdots.①\]性质若\(f\)是积性函数,则一定满足:......
  • 函数相关
    定义方式functionfoo{var=10echo$var}foo(){var=10echo$var}返回值使用return语句,但函数返回值只能是整形数值在函数体内使用echo打印变量值,可以将值输出到标准输出中,调用者通过result=${foo}或者result=`foo`的方式获取函数的输出值。......
  • Oracle - 常见函数总结
    to_date()字符串转日期selectto_date('20050101','yyyyMMdd')todayfromdualto_char()将数值或日期型转化为字符selectto_char(12345678,'999,999,999,999')fromdual;selectto_char(sysdate,'yyyy-MM-dd')fromdual;to_number()......
  • MySQL binlog日志总结
    概念描述binlog日志:binlog日志用于记录所有更新了数据或者已经潜在更新了数据(例如,没有匹配任何行的一个DELETE)的所有语句。语句以“事件”的形式保存,它描述数据更改。binlog可用于实时备份,主从复制master->slave的数据同步。知识总结binlog相关参数:log_bin:#开启binlog参数,可以指定......
  • DBeaver 使用中遇到驱动的问题并解决方案--mysql
    一、DBeaver的下载二、DBeaver的安装1、双击下载的EXE安装包,按提示选择目录进行安装即可三、DBeaver的配置1、第一次启动时会有一个弹窗,意思是添加一个连接数据库的模板,可以选择否2、点击窗口-首选项-连接-驱动-Maven,点击添加,增加仓库源地址(阿里云:https://maven.aliyun.com/......
  • C++中基类和派生类的析构函数
    和构造函数类似,析构函数也不能被继承。与构造函数不同的是,在派生类的析构函数中不用显式地调用基类的析构函数,因为每个类只有一个析构函数,编译器知道如何选择,无需程序员干涉。另外析构函数的执行顺序和构造函数的执行顺序也刚好相反:创建派生类对象时,构造函数的执行顺序和继承顺序相......
  • .NetCore6 与 MySql 联查 (使用Lambda表达式树)
    MySql 官网链接:  https://www.donet5.com/Doc/99999999999/1180    2. 落实代码  or and 并且(满足所有条件) 或者(满足其中一个条件)  查询 OrIF !非空,查询  3. 联查表  官网找方法   4.落实  我这有一个字典表......