MySQL Function

DATE_SUB

subtracting days

sql 复制代码
SELECT DATE_SUB('2025-09-05', INTERVAL 7 DAY);
-- Result: '2025-08-29'

SELECT DATE_SUB('2025-09-05', INTERVAL 3 MONTH);
-- Result: '2025-06-05'

SELECT DATE_SUB('2025-09-05', INTERVAL 1 YEAR);
-- Result: '2024-09-05'

SELECT DATE_SUB('2025-09-05 10:30:00', INTERVAL '2:15' HOUR_MINUTE);
-- Result: '2025-09-05 08:15:00'

WEEK(date , firstdayofweek) 周

Optional. Specifies what day the week starts on. Can be one of the following:

  • 0 - First day of week is Sunday
  • 1 - First day of week is Monday and the first week of the year has more than 3 days
  • 2 - First day of week is Sunday
  • 3 - First day of week is Monday and the first week of the year has more than 3 days
  • 4 - First day of week is Sunday and the first week of the year has more than 3 days
  • 5 - First day of week is Monday
  • 6 - First day of week is Sunday and the first week of the year has more than 3 days
  • 7 - First day of week is Monday

日期是第几周

sql 复制代码
SELECT WEEK('2025-06-02 10:08:29', 2 ) - WEEK(DATE_SUB('2025-06-02 10:08:29', 
INTERVAL DAYOFMONTH('2025-06-02 10:08:29') - 1 DAY), 2 )+1 AS week_of_month;

SELECT WEEK('2025-06-30 10:08:29', 2 ) - WEEK(DATE_SUB('2025-06-30 10:08:29', 
INTERVAL DAYOFMONTH('2025-06-30 10:08:29') - 1 DAY), 2 )+1 AS week_of_month;



SELECT WEEK ( '2025-06-02 10:46:46', 2 ) - 
WEEK ( DATE_FORMAT( '2025-06-02 10:46:46', '%Y-%m-01' ), 2 )+1

SELECT WEEK ( '2025-06-30 10:46:46', 2 ) - WEEK ( 
DATE_FORMAT( '2025-06-30 10:46:46', '%Y-%m-01' ), 2 )+1
相关推荐
冲上云霄的Jayden5 小时前
修改 Docker 容器中 MySQL 8.0 默认编码为 utf8mb4_unicode_ci
utf-8·mysql·ci/cd·docker·utf8mb4
一只自律的鸡5 小时前
【MySQL】第四章 排序和分页
数据库·mysql
qq_203769496 小时前
debian13安装PostgreSQL并远程连接
数据库·postgresql
苏小瀚6 小时前
[MySQL] 联合查询
数据库·mysql
雪碧聊技术6 小时前
Linux命令过关挑战
linux·运维·数据库
oak隔壁找我6 小时前
SpringBoot + MyBatis 配置详解
java·数据库·后端
oak隔壁找我6 小时前
SpringBoot + Redis 配置详解
java·数据库·后端
帧栈6 小时前
开发避坑指南(64):修复IllegalArgumentException:参数值类型与期望类型不匹配
java·数据库
麦聪聊数据6 小时前
Web原生架构如何优化数据库权限管理:简化操作与增强安全性
数据库
ldmd2847 小时前
Go语言实战:入门篇-4:与数据库、redis、消息队列、API
数据库·redis·缓存