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
相关推荐
溪语流沙8 分钟前
Django + Vue电商项目第001讲:开篇|注册登录加增删改查,那不是电商
redis·python·mysql·docker·typescript·django·vue
努力努力再努力wz18 分钟前
【Redis进阶系列】:从主从复制到 Sentinel,一文建立故障检测、Leader 选举与 Failover 的完整心智模型
数据库·redis·缓存
达梦数据26 分钟前
DMDRS搭建部署简介与运行环境
数据库
Omics Pro29 分钟前
1个月2轮融资!长寿虚拟细胞
数据库·人工智能·算法·机器学习·自然语言处理
程序员阿黄36 分钟前
基于 Django 与 Vue 3 的智能实验室预约系统设计与实现
后端·python·mysql·django·vue·毕设
zcn1261 小时前
row_number()函数与group by性能比较
数据库·sql优化改写
小K讲AI营销1 小时前
AI基础设施融资路径的中美比较:资本市场模式与政策金融模式
大数据·数据库·人工智能
净水深流1 小时前
中国冷链冷库行业数据分析:从规模扩张到技术驱动
大数据·数据库·人工智能·冷库冷链
李兆龙的博客2 小时前
从一到无穷大 #94:ClickHouse TimeSeries——时间线组织与 Prometheus 兼容性
数据库
冰暮流星2 小时前
事务的四个特性(ACID)详解
数据库