- Java锁的逻辑(结合对象头和ObjectMonitor)
- 还在用饼状图?来瞧瞧这些炫酷的百分比可视化新图形(附代码实现)⛵
- 自动注册实体类到EntityFrameworkCore上下文,并适配ABP及ABPVNext
- 基于Sklearn机器学习代码实战
整理日常业务中用到日期函数的一些场景,并对日期函数按照使用类型做了分类,实例也尽可能符合日常需求。为了方便查阅,可以先看目录,再根据需要看具体方法和实例。 首先明确日期和时间类型有哪些,也就是日期函数括号中的变量类型.
表示时间值的日期和时间类型为DATETIME、DATE、TIMESTAMP、TIME和YEAR.
类型 | 格式 | 描述 |
---|---|---|
DATE | YYYY-MM-DD | 日期值 |
TIME | HH:MM:SS | 时间值或持续时间 |
YEAR | YYYY | 年份值 |
DATETIME | YYYY-MM-DD hh:mm:ss | 混合日期和时间值 |
TIMESTAMP | YYYY-MM-DD hh:mm:ss | 混合日期和时间值,时间戳 |
函数 | 描述 |
---|---|
date(datetime) | 返回datetime中的日期date部分 |
year(date) | 返回年份 |
quarter(date) | 返回日期date是第几季节,返回 1 到 4 |
month(date) | 返回日期date中的月份值,1 到 12 |
week(date) | 计算日期 date 是本年的第几个星期,范围是 0 到 53 |
day(date) | 返回日期值 date 的日期部分 |
hour(time) | 返回 date 中的小时值 |
minute(time) | 返回 date 中的分钟值 |
second(time) | 返回 t 中的秒钟值 |
microsecond(time) | 返回日期参数所对应的微秒数 |
extract(type from date) | 从日期 date 中获取指定的值,type 指定返回的值 |
weekday(date) | 日期 date 是星期几,0 表示星期一,1 表示星期二... |
本文中type的取值:
year(年)、quarter(季)、month(月)、week(周)、day(天)、hour(小时)、minute(分钟)、second(秒)、microsecond(毫秒).
函数 | 描述 |
---|---|
year、month、day | 使用and连接单个返回值 |
date_format(event_time,’unit‘) | 按照unit取值 |
substring(date, start, length) | 从字符串 str 的 start 位置开始截取长度为 length 的子字符串 |
like | 模糊查询 |
本文中unit的取值:
%M 月名字(January……December) %W 星期名字(Sunday……Saturday) %D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。) %Y 年, 数字, 4 位 %y 年, 数字, 2 位 %a 缩写的星期名字(Sun……Sat) %d 月份中的天数, 数字(00……31) %e 月份中的天数, 数字(0……31) %m 月, 数字(01……12) %c 月, 数字(1……12) %b 缩写的月份名字(Jan……Dec) %j 一年中的天数(001……366) %H 小时(00……23) %k 小时(0……23) %h 小时(01……12) %I 小时(01……12) %l 小时(1……12) %i 分钟, 数字(00……59) %r 时间,12 小时(hh:mm:ss [AP]M) %T 时间,24 小时(hh:mm:ss) %S 秒(00……59) %s 秒(00……59) %p AM或PM %w 一个星期中的天数(0=Sunday ……6=Saturday ) %U 星期(0……52), 这里星期天是星期的第一天 %u 星期(0……52), 这里星期一是星期的第一天 %% 一个文字“%”。
例如 :date_format(%Y-%m-%d %H:%i:%s)得到时间戳 。
函数 | 描述 |
---|---|
datediff(date_1,date_2) | 返回日期 date_1 - date_2 之间相隔的天数 |
timestampdiff(type,datetime_1,datetime_2) | 返回 datetime_2 − datetime_1 的时间差 |
timediff(time_1, time_2)计算时间差值 | 返回time_1 - time_2 的时间差值 |
参数说明:
type:参数确定(end-begin)结果的单位,表示为整数。可以填year(年)、quarter(季)、month(月)、week(周)、day(天)、hour(小时)、minute(分钟)、second(秒)、microsecond(毫秒).
返回datetime_2-datetime_1的结果,其中datetime_1和datetime_2是date或者datetime表达式,也可以是混合类型; 。
当datetime_1>datetime_2时,返回的结果为负; 。
当datetime_1<datetime_2时,返回的结果为正.
- 什么时候使用datediff?什么时候使用timestampdiff?什么时候使用timediff?
三个函数的区别主要在于对象的不同一个是日期,一个是时间,另外一个既可以是日期格式也可以是时间格式。 timestampdiff更加灵活,既可以对日期求整数差也可以对时间求整数差。而datediff只能求日期的天数差。
函数 | 描述 |
---|---|
date_add(date,interval expr type) | 返回起始日期 date 加上一个时间段后的日期 |
date_sub(date,interval expr type) | 返回函数从日期减去指定的时间间隔 |
addtime(time,n) | 返回n 是一个时间表达式,时间 t 加上时间表达式 n |
subtime(time,n) | 时间 t 减去 n 秒的时间 |
函数 | 描述 |
---|---|
weekday(date) | 日期 date 是星期几,0 表示星期一,1 表示星期二... |
date_format(date,'%W') | 日期 date 是星期几,Monday周一,Tuesday周二... |
week(date) | 计算日期 date 是本年的第几个星期,范围是 0 到 53 |
函数 | 描述 |
---|---|
last_day(date) | 返回给定日期的那一月份的最后一天 |
函数 | 描述 |
---|---|
curdate()、current_date() | 返回当前日期 |
curtime()、current_time() | 返回当前时间 |
now() | 返回当前日期和时间 |
分为两部分,简单和复杂实例,简单实例是为了便于理解函数的使用,复杂实例是结合业务场景给出了常见的问题.
# 返回'2023-01-09 22:32:17'的 年月日等 部分
select
date('2023-01-09 22:32:17'); -- 2023-01-09
select
year('2023-01-09 22:32:17'); -- 2023
select
quarter('2023-01-09 22:32:17'); -- 1
select
month('2023-01-09 22:32:17'); -- 1
select
week('2023-01-09 22:32:17'); -- 2(本年第二周)
select
day('2023-01-09 22:32:17'); -- 9
select
hour('2023-01-09 22:32:17'); -- 22
select
minute('2023-01-09 22:32:17'); -- 32
select
second('2023-01-09 22:32:17'); -- 17
select
extract(day from '2023-01-09 22:32:17'); -- 9
select
weekday('2023-01-09 22:32:17'); -- 0(周一)
# 返回'2023-01-09 22:32:17'的 年月、年月日等部分
select
date_format('2023-01-09 22:32:17','%Y%m'); -- 202301
select
date_format('2023-01-09 22:32:17','%y%m'); -- 2301
select
date_format('2023-01-09 22:32:17','%Y-%m-%d'); -- 2023-01-09
select
date_format('2023-01-09 22:32:17','%H:%i'); -- 22:32
select
date_format('2023-01-09 22:32:17','%s'); -- 17
select
substring('2023-01-09 22:32:17',1,7); -- 2023-01
select
substring('2023-01-09 22:32:17',1,10); -- 2023-01-09
select
substring('2023-01-09 22:32:17',12,5); -- 22:32
select
substring('2023-01-09 22:32:17',18,2); -- 17
# 返回'2023-01-09 22:32:17'和 '2023-03-15 04:18:56'的 整数差或者和
select
datediff('2023-01-09 22:32:17','2023-03-15 04:18:56'); -- -65
select
datediff('2023-03-15 04:18:56','2023-01-09 22:32:17'); -- 65
select
timediff('2023-01-09 22:32:17','2023-03-15 04:18:56'); -- -838:59:59
select
timediff('2023-03-15 04:18:56','2023-01-09 22:32:17'); -- 838:59:59
select
timestampdiff(year,'2023-01-09 22:32:17','2023-03-15 04:18:56'); -- 0
select
timestampdiff(year,'2023-03-15 04:18:56','2023-01-09 22:32:17'); -- 0
select
timestampdiff(quarter,'2023-01-09 22:32:17','2023-03-15 04:18:56'); -- 0
select
timestampdiff(quarter,'2023-03-15 04:18:56','2023-01-09 22:32:17'); -- 0
select
timestampdiff(month,'2023-01-09 22:32:17','2023-03-15 04:18:56'); -- 2
select
timestampdiff(month,'2023-03-15 04:18:56','2023-01-09 22:32:17'); -- -2
select
timestampdiff(week,'2023-01-09 22:32:17','2023-03-15 04:18:56'); -- 9
select
timestampdiff(week,'2023-03-15 04:18:56','2023-01-09 22:32:17'); -- -9
select
timestampdiff(day,'2023-01-09 22:32:17','2023-03-15 04:18:56'); -- 64
select
timestampdiff(day,'2023-03-15 04:18:56','2023-01-09 22:32:17'); -- -64
select
timestampdiff(hour,'2023-01-09 22:32:17','2023-03-15 04:18:56'); -- 1541
select
timestampdiff(hour,'2023-03-15 04:18:56','2023-01-09 22:32:17'); -- -1541
select
timestampdiff(minute,'2023-01-09 22:32:17','2023-03-15 04:18:56'); -- 92506
select
timestampdiff(minute,'2023-03-15 04:18:56','2023-01-09 22:32:17'); -- -92506
select
timestampdiff(second,'2023-01-09 22:32:17','2023-03-15 04:18:56'); -- 5550399
select
timestampdiff(second,'2023-03-15 04:18:56','2023-01-09 22:32:17'); -- -5550399
# 返回日期'2023-01-09 22:32:17'加上或者减去 某个时间段 的日期
select
date_add('2023-01-09 22:32:17',interval 2 year); -- 2025-01-09 22:32:17
select
date_add('2023-01-09 22:32:17',interval 2 quarter); -- 2023-07-09 22:32:17
select
date_add('2023-01-09 22:32:17',interval 2 month); -- 2023-03-09 22:32:17
select
date_add('2023-01-09 22:32:17',interval 2 week); -- 2023-01-23 22:32:17
select
date_add('2023-01-09 22:32:17',interval 2 day); -- 2023-01-11 22:32:17
select
date_add('2023-01-09 22:32:17',interval 2 hour); -- 2023-01-10 00:32:17
select
date_add('2023-01-09 22:32:17',interval 2 second); -- 2023-01-09 22:32:19
select
date_add('2023-01-09 22:32:17',interval 2 minute); -- 2023-01-09 22:34:17
select
date_sub('2023-01-09 22:32:17',interval 2 year); -- 2025-01-09 22:32:17
select
date_sub('2023-01-09 22:32:17',interval 2 quarter); -- 2023-07-09 22:32:17
select
date_sub('2023-01-09 22:32:17',interval 2 month); -- 2023-03-09 22:32:17
select
date_sub('2023-01-09 22:32:17',interval 2 week); -- 2023-01-23 22:32:17
select
date_sub('2023-01-09 22:32:17',interval 2 day); -- 2023-01-11 22:32:17
select
date_sub('2023-01-09 22:32:17',interval 2 hour); -- 2023-01-10 00:32:17
select
date_sub('2023-01-09 22:32:17',interval 2 second); -- 2023-01-09 22:32:19
select
date_sub('2023-01-09 22:32:17',interval 2 minute); -- 2023-01-09 22:34:17
# 时间格式默认秒
select
addtime('2023-01-09 22:32:17','22:12:00'); -- 2023-01-10 20:44:17
select
addtime('2023-01-09 22:32:17','22:12'); -- 2023-01-10 20:44:17
select
addtime('2023-01-09 22:32:17','22'); -- 2023-01-09 22:32:39
select
subtime('2023-01-09 22:32:17','22:12:00'); -- 2023-01-09 00:20:17
select
subtime('2023-01-09 22:32:17','22:12'); -- 2023-01-09 00:20:17
select
subtime('2023-01-09 22:32:17','22'); -- 2023-01-09 22:31:55
# 返回日期'2023-01-09 22:32:17' 是周几
select
weekday('2023-01-09 22:32:17'); -- 0(周一)
select
week('2023-01-09 22:32:17'); -- 2(本年第二周)
select
date_format('2023-01-09 22:32:17','%W'); -- Monday(周一的英文)
# 返回日期'2023-01-09 22:32:17' 该月最后一天
select
last_day('2023-01-09 22:32:17'); -- 2023-01-31
# 返回当前日期或者时间
select
curdate(), -- 2023-01-23
current_date(), -- 2023-01-23
curtime(), -- 23:32:37
current_time(), -- 23:32:37
now(); -- 2023-01-23 23:32:37
students 表 (id表示主键,student_id是学号,score是平均成绩,date是作答日期) 。
id | student_id | score | answer_date |
---|---|---|---|
1 | 1123 | 98 | 2023-01-08 |
2 | 2341 | 74 | 2022-02-12 |
3 | 5623 | 66 | 2022-12-23 |
4 | 4386 | 80 | 2023-01-12 |
#使用month、day
select
month(answer_date) as month,
count(answer_date) as count
from students_score
where day(answer_date) = 12
group by month;
month count
1 1
2 1
#1、使用year、month、day
select
count(answer_date)
from students_score
where month(answer_date) = 1 and year(answer_date) = 2023;
#2、使用date_format
select
count(answer_date)
from students_score
where date_format(answer_date,'%Y%m') = '202301';
#3、使用substring
select
count(answer_date)
from students_score
where substring(answer_date,1,7) = '2023-01';
#4、使用like
select
count(answer_date)
from students_score
where answer_date like '2023-01%';
#结果:2
select week('2022-02-12'); #6(表示第六周)
select date_format('2023-01-05','%W'); #Thursday(表示周四)
#1、使用weekday
select
student_id,
weekday(answer_date) as week
from students_score
where weekday(answer_date) < 5;
#2、使用date_format(date,'%W')
select
student_id,
date_format(answer_date,'%W') as week
from students_score
where date_format(answer_date,'%W') in ('Monday','Tuesday','Wednesday','Thursday','Friday');
注意两种方法的区别 。
student_id week
5623 4
4386 3
5623 Friday
4386 Thursday
select
date_format(answer_date,'%Y%m') as date_,
count(answer_date)/day(last_day(answer_date)) as avg_num
from students_score
group by date_;
date_ avg_num
202202 0.0357
202212 0.0323
202301 0.0645
如何计算近7天和近30天的日期:日期减去天数得到日期;另一种是日期减去日期得到天数.
#先求出今日是哪一天(最大日期),往前七天是哪一天,往前30天是哪一天
with
main as(
select
max(date(answer_date)) as now_date,
date_sub(max(date(answer_date)),interval 6 day) as 7_date,
date_sub(max(date(answer_date)),interval 29 day) as 30_date
from students_score
)
select
sum(if(datediff(answer_date,7_date)) >= 0,1,0) as ans_7_num,
sum(if(datediff(answer_date,30_date)) >= 0,1,0) as ans_30_num
from students_score, main
ans_7_num ans_30_num
2 3
创建 students_score 表的代码 。
-- ----------------------------
-- Table structure for students_score
-- ----------------------------
DROP TABLE IF EXISTS `students_score`;
CREATE TABLE `students_score` (
`id` int(11) NOT NULL COMMENT '主键',
`student_id` int(11) NOT NULL COMMENT '学号',
`score` int(11) NULL DEFAULT NULL COMMENT '成绩',
`date` date NOT NULL COMMENT '作答日期',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of students_score
-- ----------------------------
INSERT INTO `students_score` VALUES (1, 1123, 98, '2023-01-08');
INSERT INTO `students_score` VALUES (2, 2341, 74, '2023-01-12');
INSERT INTO `students_score` VALUES (3, 5623, 66, '2022-12-23');
INSERT INTO `students_score` VALUES (4, 4386, 80, '2023-01-12');
最后此篇关于MySQL日期函数、时间函数在实际场景中的应用的文章就讲到这里了,如果你想了解更多关于MySQL日期函数、时间函数在实际场景中的应用的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在通过 labrepl 工作,我看到了一些遵循此模式的代码: ;; Pattern (apply #(apply f %&) coll) ;; Concrete example user=> (a
我从未向应用商店提交过应用,但我会在不久的将来提交。 到目前为止,我对为 iPhone 而非 iPad 进行设计感到很自在。 我了解,通过将通用PAID 应用放到应用商店,客户只需支付一次就可以同时使
我有一个应用程序,它使用不同的 Facebook 应用程序(2 个不同的 AppID)在 Facebook 上发布并显示它是“通过 iPhone”/“通过 iPad”。 当 Facebook 应用程序
我有一个要求,我们必须通过将网站源文件保存在本地 iOS 应用程序中来在 iOS 应用程序 Webview 中运行网站。 Angular 需要服务器来运行应用程序,但由于我们将文件保存在本地,我们无法
所以我有一个单页客户端应用程序。 正常流程: 应用程序 -> OAuth2 服务器 -> 应用程序 我们有自己的 OAuth2 服务器,因此人们可以登录应用程序并获取与用户实体关联的 access_t
假设我有一个安装在用户设备上的 Android 应用程序 A,我的应用程序有一个 AppWidget,我们可以让其他 Android 开发人员在其中以每次安装成本为基础发布他们的应用程序推广广告。因此
Secrets of the JavaScript Ninja中有一个例子它提供了以下代码来绕过 JavaScript 的 Math.min() 函数,该函数需要一个可变长度列表。 Example:
当我分别将数组和对象传递给 function.apply() 时,我得到 NaN 的 o/p,但是当我传递对象和数组时,我得到一个数字。为什么会发生这种情况? 由于数组也被视为对象,为什么我无法使用它
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章ASP转换格林威治时间函数DateDiff()应用由作者收集整理,如果你
我正在将列表传递给 map并且想要返回一个带有合并名称的 data.frame 对象。 例如: library(tidyverse) library(broom) mtcars %>% spl
我有一个非常基本的问题,但我不知道如何实现它:我有一个返回数据框,其中每个工具的返回值是按行排列的: tmp<-as.data.frame(t(data.frame(a=rnorm(250,0,1)
我正在使用我的 FB 应用创建群组并邀请用户加入我的应用群组,第一次一切正常。当我尝试创建另一个组时,出现以下错误: {"(OAuthException - #4009) (#4009) 在有更多用户
我们正在开发一款类似于“会说话的本”应用程序的 child 应用程序。它包含大量用于交互式动画的 JPEG 图像序列。 问题是动画在 iPad Air 上播放正常,但在 iPad 2 上播放缓慢或滞后
我关注 clojure 一段时间了,它的一些功能非常令人兴奋(持久数据结构、函数式方法、不可变状态)。然而,由于我仍在学习,我想了解如何在实际场景中应用,证明其好处,然后演化并应用于更复杂的问题。即,
我开发了一个仅使用挪威语的应用程序。该应用程序不使用本地化,因为它应该仅以一种语言(挪威语)显示。但是,我已在 Info.plist 文件中将“本地化 native 开发区域”设置为“no”。我还使用
读完 Anthony's response 后上a style-related parser question ,我试图说服自己编写单体解析器仍然可以相当紧凑。 所以而不是 reference ::
multicore 库中是否有类似 sapply 的东西?还是我必须 unlist(mclapply(..)) 才能实现这一点? 如果它不存在:推理是什么? 提前致谢,如果这是一个愚蠢的问题,我们深表
我喜欢在窗口中弹出结果,以便更容易查看和查找(例如,它们不会随着控制台继续滚动而丢失)。一种方法是使用 sink() 和 file.show()。例如: y <- rnorm(100); x <- r
我有一个如下所示的 spring mvc Controller @RequestMapping(value="/new", method=RequestMethod.POST) public Stri
我正在阅读 StructureMap关于依赖注入(inject),首先有两部分初始化映射,具体类类型的接口(interface),另一部分只是实例化(请求实例)。 第一部分需要配置和设置,这是在 Bo
我是一名优秀的程序员,十分优秀!