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
相关推荐
nvd118 分钟前
深入分析:Pytest异步测试中的数据库会话事件循环问题
数据库·pytest
appearappear14 分钟前
如何安全批量更新数据库某个字段
数据库
·云扬·1 小时前
MySQL 常见存储引擎详解及面试高频考点
数据库·mysql·面试
羊小猪~~1 小时前
【QT】--文件操作
前端·数据库·c++·后端·qt·qt6.3
coding-fun2 小时前
电子发票批量提取导出合并助手
大数据·数据库
leo_2322 小时前
备份&恢复--SMP(软件制作平台)语言基础知识之三十九
数据库·数据安全·开发工具·smp(软件制作平台)·应用系统
何以不说话2 小时前
mysql 的主从复制
运维·数据库·学习·mysql
二二牧人2 小时前
qemu arm64 linux开发环境搭建
linux·运维·数据库
茁壮成长的露露2 小时前
导出导入工具mongoexport、mongoimport
数据库·mongodb
Coder_Boy_2 小时前
基于SpringAI的在线考试系统-考试系统DDD(领域驱动设计)实现步骤详解
java·数据库·人工智能·spring boot