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
相关推荐
塔能物联运维4 分钟前
存量机房升级成为行业主流方向:热管理重构算力中心价值路径
数据库
lqj_本人8 分钟前
鸿蒙electron跨端框架PC工志簿实战:项目、工时、阻塞和下一步都要有位置
数据库·华为·harmonyos
刘一说26 分钟前
AI科技热点日报 | 2026年5月22日
数据库·人工智能·科技
LCG元33 分钟前
RAG工程指南:从基础检索到生产部署全解析
java·运维·数据库
godspeed_lucip1 小时前
LLM和Agent——专题3: Agentic Workflow 入门(1)
大数据·数据库·人工智能
南境十里·墨染春水1 小时前
讲讲IO复用三个函数的底层逻辑
数据库
吴可可1231 小时前
Teigha处理CAD样条曲线的方法解析
数据库·算法·c#
这个DBA有点耶1 小时前
数据迁移避坑指南:从Oracle到国产数据库的兼容性问题
数据库·数据仓库·sql·oracle·dba
小短腿的代码世界2 小时前
Qt国际化深度解析:从源码到企业级多语言实践
java·数据库·qt
Ting-yu2 小时前
Spring AI Alibaba零基础速成(6) ---- 向量化
数据库·人工智能