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
相关推荐
WL_Aurora3 分钟前
MySQL 5 卸载到 MySQL 8 安装完整指南(不踩坑版)
数据库·mysql
灰阳阳6 分钟前
MySQL的基本架构
数据库·mysql·架构
@小柯555m25 分钟前
MySql(高级操作符--Where in 和Not in)
数据库·sql·mysql
许彰午26 分钟前
CacheSQL(一):手写数据库的工程化重生
java·数据库·缓存
MmeD UCIZ26 分钟前
MySQL单表存多大的数据量比较合适
数据库·mysql
SarL EMEN1 小时前
mysql之联合索引
数据库·mysql
l1t1 小时前
DeepSeek总结的DuckDB anofox-forecast季节调整时间序列预测插件功能
开发语言·数据库
meta INGU1 小时前
mysql数据被误删的恢复方案
数据库·mysql
27669582921 小时前
acw_sc__v2
数据库·acw_sc__v2·acw_sc__v2逆向·acw_sc__v2算法分析·acw_sc__v2分析·acw_sc__v2还原·acw_sc__v2-ck
Empty-Filled1 小时前
Claude Gateway 排查教程
网络·数据库·人工智能