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
相关推荐
歪歪1009 小时前
如何在SQLite中实现事务处理?
java·开发语言·jvm·数据库·sql·sqlite
瑶总迷弟9 小时前
静默安装 Oracle Database 21c on CentOS 7.6
数据库·oracle·centos
博睿谷IT99_11 小时前
SQL SELECT 语句怎么用?COMPANY 表查询案例(含条件 / 模糊 / 分页)
数据库·sql·mysql
浅拾光º11 小时前
mysql字符串截取,如何在MySQL备份文件中安全截取敏感字符串?
数据库·mysql·安全
鸠摩智首席音效师11 小时前
如何删除 MySQL 数据库中的所有数据表 ?
数据库·mysql·oracle
Hey! Hey!11 小时前
DBA 系统学习计划(从入门到进阶)
数据库·学习·dba
leo_yu_yty11 小时前
Mysql DBA学习笔记(客户端常用工具)
学习·mysql·dba
没有bug.的程序员11 小时前
SQL 执行计划解析:从 EXPLAIN 到性能优化的完整指南
java·数据库·sql·性能优化·explain·执行计划
qqxhb11 小时前
系统架构设计师备考第30天——关系数据库
数据库·系统架构·数据库设计·关系模式·范式·关系远算·完整性规则
小心草里有鬼12 小时前
Linux 数据库 Mysql8 主从复制
linux·运维·数据库·sql·mysql