企业级应用 mysql 日期函数变量,干货已整理

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 本文详细介绍了如何在MySQL8.0中使用DATE_FORMAT函数进行日期格式的转换,包括当日、昨日及不同时间段的数据获取,并提供了实际的ETL应用场景和注意事项,有助于提升数据处理的灵活性和一致性。

企业级应用 mysql 8.0 日期函数变量,干货已整理

曾经也做过几年的 数据开发,都会用 时间函数做为变量重刷数据 。比如重刷去年的数据,重刷当月的数据等

今天整理一下干货


前言

[具体可以访问 mysql Data_format 帮助]

DATE_FORMAT 是 MySQL 中用于格式化日期的函数。它允许你将日期按照指定的格式进行显示。以下是 DATE_FORMAT 函数的基本语法:

DATE_FORMAT(date, format)

其中:

date 是要格式化的日期值。

format 是用于指定日期格式的格式字符串。

下面是一些常用的日期格式元素,它们可以在 format 字符串中使用:

Specifier Description
%a Abbreviated weekday name (Sun…Sat)
%b Abbreviated month name (Jan…Dec)
%c Month, numeric (0…12)
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d Day of the month, numeric (00…31)
%e Day of the month, numeric (0…31)
%f Microseconds (000000…999999)
%H Hour (00…23)
%h Hour (01…12)
%I Hour (01…12)
%i Minutes, numeric (00…59)
%j Day of year (001…366)
%k Hour (0…23)
%l Hour (1…12)
%M Month name (January…December)
%m Month, numeric (00…12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00…59)
%s Seconds (00…59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00…53), where Sunday is the first day of the week; WEEK() mode 0
%u Week (00…53), where Monday is the first day of the week; WEEK() mode 1
%V Week (01…53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
%v Week (01…53), where Monday is the first day of the week; WEEK() mode 3; used with %x
%W Weekday name (Sunday…Saturday)
%w Day of the week (0=Sunday…6=Saturday)
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal % character
%x x, for any “x” not listed above

一、当日

select 
now() as start_time--当前日期 
date_format(CURDATE(),'%Y-%m-%d') cur_date,-- 当日
date_format(CURDATE(),'%Y-%m') cur_yyyymm-- 当日对应的年月

二、昨日_1

select 
date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m-%d') last_date, -- 昨日
date_format(date_sub(CURDATE(), interval 1 day),'%Y%m%d') last_date8, -- 昨日yyyymmdd格式
date_format(DATE_SUB(CURDATE(), interval 3 day),'%Y-%m-%d') last_day_2, -- 昨日前推2天
date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m') as cur_month,-- 昨日对应的月份
date_format(date_sub(CURDATE(), interval 1 day),'%Y') as cur_year,-- 昨日对应的年份
date_format(date_add(date_sub(CURDATE(),interval 1 day),interval-1 year),'%Y') last_year,-- 昨日对应的去年

三、昨日_2

select 
date_format(date_add(date_sub(CURDATE(), interval 1 day),interval 1 month),'%Y-%m') as next_month,-- 昨日对应月份的下一个月
date_format(date_add(date_sub(CURDATE(), interval 1 day),interval 2 month),'%Y-%m') as next_month2,-- 昨日对应月份的下2个月
date_format(date_add(date_sub(CURDATE(), interval 1 day),interval -1 month),'%Y-%m') as last_month,-- 昨日对应月份的上一个月
date_format(date_add(date_sub(CURDATE(), interval 1 day),interval -2 month),'%Y-%m') as last_month2,-- 昨日对应月份的上一个月
date_sub(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m'),'-01'),interval 1 day) as end_date_last_month, -- 昨日对应上月的月底时间,即昨日对应月初-1天
date_format(date_sub(date_sub(CURDATE(), interval 1 day),interval 3 month),'%Y-%m') last_month_3, -- 昨日对应的当月前推3个月
date_format(date_sub(date_sub(CURDATE(), interval 1 day),interval 6 month),'%Y-%m') last_month_6, -- 昨日对应的当月前推6个月
date_format(date_sub(date_sub(CURDATE(), interval 1 day),interval 9 month),'%Y-%m') last_month_9, -- 昨日对应的当月前推9个月
date_format(date_sub(date_sub(CURDATE(), interval 1 day),interval 12 month),'%Y-%m') last_month_12, -- 昨日对应的当月前推12个月
date_format(date_sub(date_sub(CURDATE(), interval 1 day),interval 24 month),'%Y-%m') last_month_24 -- 昨日对应的当月前推24个月


三、昨日_3

select 
date_format(date_sub(CURDATE(), interval 8 day),'%Y-%m-%d') last_date_7, -- 昨日的一个星期前
date_format(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m'),'-01'),'%Y-%m-%d')as first_month_date,-- 昨日的月初
date_format(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y%m'),'01'),'%Y%m%d') as first_month_date8,-- 昨日的月初_yyymmdd格式  
date_add(date_format(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m'),'-01'),'%Y-%m-%d'),interval 1 month) as next_first_month_date,-- 昨日的下月初1号
date_format(date_sub(date_add(date_format(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m'),'-01'),'%Y-%m-%d'),interval 1 month),interval 1 day),'%Y-%m-%d') as end_month_date,-- 昨日对应的本月月末最后一天
date_format(date_sub(date_add(date_format(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y-%m'),'-01'),'%Y-%m-%d'),interval -11 month),interval 1 day),'%Y-%m-%d') as last_end_month_date,-- 昨日对应的去年本月月末最后一天
concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y'),'-01') as first_month_year,-- 昨日对应的本年第一个月
concat(date_format(date_sub(date_sub(CURDATE(), interval 1 day),interval 1 year),'%Y'),'-01') as first_month_last_year-- 昨日对应的去年第一个月

三、昨日_4

select
date_format(date_sub(date_sub(CURDATE(),interval 1 year),interval 1 day),'%Y-%m-%d') as day_of_last_year,---- 昨日对应的去年相同年月日
date_format(date_add(date_sub(CURDATE(),interval 1 day),interval-1 year),'%Y-%m') yyyymm_of_last_year,-- 昨日对应的去年同样年月
date_format(date_add(date_sub(CURDATE(),interval 1 day),interval-2 year),'%Y-%m-%d') as day_of_last_2year, -- 昨日对应的前年同样日期 
date_format(date_add(date_sub(CURDATE(),interval 1 day),interval-2 year),'%Y-%m') yyyymm_of_last_2year, -- 昨日对应的前年同样年月 
date_sub(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y'),'-01-01'), interval 1 day) as last_end_year,-- 昨日对应去年的最后一天
date_sub(date_sub(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y'),'-01-01'), interval 1 day),interval 1 year) as last_end_year2,-- 昨日对应前去年的最后一天
date_format(concat(date_format(date_sub(CURDATE(), interval 1 day),'%Y'),'-01-01'),'%Y-%m-%d') as first_year_date -- 昨日对应前今年的第一天

三、昨日_5

select
date_format(date_sub(date_sub(CURDATE(),interval 1 year),interval 1 day),'%Y-%m-%d') as last_date1,-- 昨日对应的去年今天
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 1 year),interval 1 day),'%Y-%m'),'-01'),'%Y-%m') as first_month_date1,-- 昨日对应的去年年月
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 1 year),interval 1 day),'%Y') ,'-01-01'),'%Y-%m-%d') as first_year_date1,-- 昨日对应的去年第一天
date_format(date_sub(date_sub(CURDATE(),interval 2 year),interval 1 day),'%Y-%m-%d') as last_date2,-- 昨日对应的前年今天
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 2 year),interval 1 day),'%Y-%m'),'-01'),'%Y-%m') as first_month_date2,-- 昨日对应的前年的年月
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 2 year),interval 1 day),'%Y') ,'-01-01'),'%Y-%m-%d') as first_year_date2,-- 昨日对应的前年第一天
date_format(date_sub(date_sub(CURDATE(),interval 3 year),interval 1 day),'%Y-%m-%d') as last_date3, -- 昨日对应的往前推 3年的 今天
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 3 year),interval 1 day),'%Y-%m'),'-01'),'%Y-%m') as first_month_date3,-- 昨日对应的往前推3年的年月
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 3 year),interval 1 day),'%Y') ,'-01-01'),'%Y-%m-%d') as first_year_date3,-- 昨日对应的往前推3年第一天
date_format(date_sub(date_sub(CURDATE(),interval 4 year),interval 1 day),'%Y-%m-%d') as last_date4, -- 昨日对应的往前推 4年的 今天
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 4 year),interval 1 day),'%Y-%m'),'-01'),'%Y-%m') as first_month_date4, -- 昨日对应的往前推4年的年月
date_format(concat(date_format(date_sub(date_sub(CURDATE(),interval 4 year),interval 1 day),'%Y') ,'-01-01'),'%Y-%m-%d') as first_year_date4 -- 昨日对应的往前推4年第一天

总结

以上就整理的根据昨天或今日计算出年,月,日。

应用场景:

转换日期格式以适应特定的应用需求。

生成报告中的易读日期字符串。

在将日期数据导出到外部系统时,确保日期格式的一致性。

ETL 示例:


ETL 过程,从源表提取数据,进行转换,然后加载到目标表

INSERT INTO destination_table (id, formatted_date)
SELECT
    id,
    DATE_FORMAT(original_date, '%Y-%m-%d') AS formatted_date
FROM
    source_table;

注意事项:

日期格式字符串中的各种元素之间可以添加自定义字符,如空格、短横线等,以获得所需的格式。


要确保格式字符串与实际日期数据的格式相匹配,以避免错误的格式化结果。

总体而言,DATE_FORMAT 函数在处理日期数据时是一个非常有用的工具,可以根据具体需求灵活调整格式,提高数据的可读性和适应性。


最后

希望大家喜欢 一建3连 ,初十快乐


相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
11天前
|
存储 自然语言处理 关系型数据库
MySQL 自定义变量并声明字符编码
MySQL 自定义变量并声明字符编码
27 1
|
2天前
|
JSON 关系型数据库 MySQL
Mysql(5)—函数
MySQL提供了许多内置的函数以帮助用户进行数据操作和分析。这些函数可以分为几类,包括聚合函数、字符串函数、数值函数、日期和时间函数、控制流函数等。
13 1
Mysql(5)—函数
|
10天前
|
关系型数据库 MySQL 数据库
MySQL数据库:基础概念、应用与最佳实践
一、引言随着互联网技术的快速发展,数据库管理系统在现代信息系统中扮演着核心角色。在众多数据库管理系统中,MySQL以其开源、稳定、可靠以及跨平台的特性受到了广泛的关注和应用。本文将详细介绍MySQL数据库的基本概念、特性、应用领域以及最佳实践,帮助读者更好地理解和应用MySQL数据库。二、MySQL
33 5
|
11天前
|
存储 SQL 关系型数据库
MySQL 存储函数及调用
MySQL 存储函数及调用
13 3
|
12天前
|
缓存 关系型数据库 MySQL
MySQL 满足条件函数中使用查询最大值函数
MySQL 满足条件函数中使用查询最大值函数
35 1
|
1天前
|
架构师 关系型数据库 MySQL
MySQL最左前缀优化原则:深入解析与实战应用
【10月更文挑战第12天】在数据库架构设计与优化中,索引的使用是提升查询性能的关键手段之一。其中,MySQL的最左前缀优化原则(Leftmost Prefix Principle)是复合索引(Composite Index)应用中的核心策略。作为资深架构师,深入理解并掌握这一原则,对于平衡数据库性能与维护成本至关重要。本文将详细解读最左前缀优化原则的功能特点、业务场景、优缺点、底层原理,并通过Java示例展示其实现方式。
7 1
|
27天前
|
数据采集 关系型数据库 MySQL
MySQL表约束的种类与应用
在设计数据库时,合理应用各种约束对于创建一个结构化良好且能够有效维护数据完整性的数据库至关重要。每种约束类型都有其特定的应用场景,理解并正确应用这些约束,可以大大提高数据库应用的稳定性和性能。
33 3
|
4天前
|
存储 SQL 关系型数据库
Mysql学习笔记(二):数据库命令行代码总结
这篇文章是关于MySQL数据库命令行操作的总结,包括登录、退出、查看时间与版本、数据库和数据表的基本操作(如创建、删除、查看)、数据的增删改查等。它还涉及了如何通过SQL语句进行条件查询、模糊查询、范围查询和限制查询,以及如何进行表结构的修改。这些内容对于初学者来说非常实用,是学习MySQL数据库管理的基础。
22 6
|
2天前
|
存储 关系型数据库 MySQL
Mysql(4)—数据库索引
数据库索引是用于提高数据检索效率的数据结构,类似于书籍中的索引。它允许用户快速找到数据,而无需扫描整个表。MySQL中的索引可以显著提升查询速度,使数据库操作更加高效。索引的发展经历了从无索引、简单索引到B-树、哈希索引、位图索引、全文索引等多个阶段。
16 3
Mysql(4)—数据库索引
|
4天前
|
SQL Ubuntu 关系型数据库
Mysql学习笔记(一):数据库详细介绍以及Navicat简单使用
本文为MySQL学习笔记,介绍了数据库的基本概念,包括行、列、主键等,并解释了C/S和B/S架构以及SQL语言的分类。接着,指导如何在Windows和Ubuntu系统上安装MySQL,并提供了启动、停止和重启服务的命令。文章还涵盖了Navicat的使用,包括安装、登录和新建表格等步骤。最后,介绍了MySQL中的数据类型和字段约束,如主键、外键、非空和唯一等。
13 3
Mysql学习笔记(一):数据库详细介绍以及Navicat简单使用