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
相关推荐
白鹭3 小时前
MySQL主从复制进阶(GTID复制,半同步复制)
linux·运维·数据库·mysql·集群
叫我Zoe就行3 小时前
MySQL集群——主从复制
linux·数据库·学习·mysql
叫我龙翔4 小时前
MySQL】从零开始了解数据库开发 --- 表的操作
数据库·mysql·数据库开发
叫我龙翔4 小时前
【MySQL】从零开始了解数据库开发 --- 初步认识数据库
数据库·mysql·数据库开发
Apache IoTDB5 小时前
9.4 直播预告|工业时序数据库:从采数到智能决策
数据库·时序数据库
云飞云共享云桌面5 小时前
SolidWorks对电脑的硬件配置要求具体有哪些
java·服务器·前端·网络·数据库
周杰伦的稻香5 小时前
MySQL抛出的Public Key Retrieval is not allowed
数据库·mysql
2301_780789666 小时前
渗透测试与网络安全审计的关系
网络·数据库·安全·web安全·网络安全
罗光记6 小时前
Karmada v1.15 版本发布
数据库·百度·facebook·oneapi·segmentfault