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
相关推荐
折哥的程序人生 · 物流技术专研5 小时前
Java面试85题图解版 · 特别篇:2026后端高频面试题复盘(算法底层逻辑+高并发架构设计全解析,附Java实战代码)
java·网络·数据库·算法·面试
AOwhisky5 小时前
Redis 学习笔记(第三期):持久化与主从复制
运维·数据库·redis·笔记·学习·云计算
李白的天不白5 小时前
数据库连接报错问题
数据库
一条泥憨鱼5 小时前
【Redis】数据类型和常用命令
java·数据库·redis·后端·缓存
爱喝水的鱼丶6 小时前
SAP-ABAP:SAP视图开发入门:四类标准视图的适用场景与创建步骤详解
服务器·数据库·性能优化·sap·abap
大白要努力!6 小时前
MySQL 8.0 + Navicat 完整操作指南
数据库·mysql
云絮.7 小时前
数据库操作
数据库·mysql·算法·oracle
小小工匠7 小时前
Redis 缓存替换策略:8 种淘汰策略与 LRU 实现剖析
数据库·redis·缓存
设计师小聂!9 小时前
宝塔 Linux 面板保姆级教程
linux·mysql·开源·运维开发