当前位置:网站首页>MySQL INTERVAL Keyword Guidelines
MySQL INTERVAL Keyword Guidelines
2022-08-01 16:15:00 【allway2】
在本教程中,我们将了解 MySQL INTERVAL Keywords and their values to perform operations on date and time arithmetic.那么,让我们开始吧!
MySQL INTERVAL 简介
在 MySQL 中,INTERVAL Keywords are used to add or subtract date and time values.例如,If you want to add five days to the current day and display the new date,您可以使用 INTERVAL 关键字.
MySQL gives us a bunch of interval values.We'll see a full list of these interval values below.
但首先,让我们看看如何使用 interval 关键字.
INTERVAL exp UNIT;
这里,exp 表示表达式,例如 1、2、100、200 or other unit-dependent expressions.Units are the units of measure in which dates and times are calculated,如日、时、分、秒等.
在上面的语法中,INTERVAL Both keywords and unit names are case-insensitive.
We can add or subtract interval values,如下所示 -
date + INTERVAL exp UNIT
date - INTERVAL exp UNIT
请注意,The date you specify must be in a valid datetime format.
Interval values can also be used with other datetime functions,例如 ADDDATE()、DATE_ADD()、DATE_SUB()、TIMESTAMPDIFF() 等.
Format of expressions and units
下表显示了 exp 和 UNIT 的标准格式 -
Unit | exp |
---|---|
DAY | DAYS |
DAY_HOUR | DAYS HOURS |
DAY_MICROSECOND | DAYS HOURS:MINUTES:SECONDS.MICROSECONDS |
DAY_MINUTE | DAYS HOURS:MINUTES |
DAY_SECOND | DAYS HOURS:MINUTES:SECONDS |
HOUR | HOURS |
HOUR_MICROSECOND | HOURS:MINUTES:SECONDS.MICROSECONDS |
HOUR_MINUTE | HOURS:MINUTES |
HOUR_SECOND | HOURS:MINUTES:SECONDS |
MICROSECOND | MICROSECONDS |
MINUTE | MINUTES |
MINUTE_MICROSECOND | MINUTES:SECONDS.MICROSECONDS |
MINUTE_SECOND | MINUTES:SECONDS |
MONTH | MONTHS |
QUARTER | QUARTERS |
SECOND | SECONDS |
SECOND_MICROSECOND | ‘SECONDS.MICROSECONDS’ |
WEEK | WEEKS |
YEAR | YEARS |
YEAR_MONTH | ‘YEARS-MONTHS’ |
MySQL INTERVAL 示例
在第一个示例中,We'll add it for the current date 1 天.当前日期是 2022-07-05 (YYYY-MM-DD),So the output must be 2022-07-06.
SELECT CURDATE() + INTERVAL 1 DAY;
Add day interval
You can also subtract intervals from dates.此外,You can also specify your own date and time,But it must be in a valid datetime format.Check the example below -
SELECT '2022-01-01' - INTERVAL 1 DAY;
在这里,We specify the first day of the year,and try to subtract from it 1 天.The output should contain the previous year.让我们看看输出.
Subtract the day interval
如您所见,We got the expected output.
Let's see how to use interval values with other datetime functions.
SELECT DATE_ADD('2022-01-01',INTERVAL 10 DAY);
SELECT DATE_SUB('2022-01-01',INTERVAL 10 DAY);
在这里,我们使用了 DATE_ADD() 和 DATE_SUB() Function to add and subtract interval values for specified dates.
让我们看看输出.
Date addition and date sub-functions
正如你在这里看到的,We got the correct output.
MySQL INTERVAL 实例
Now we'll see some practical examples of using interval values.
We will create a form,In it we will store the member's data along with their registration date and membership expiry date.
我们将使用 DATE_ADD() The function calculates the expiration date.
CREATE TABLE members(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
reg_date DATE DEFAULT(CURDATE()),
exp_date DATE DEFAULT(DATE_ADD(reg_date,INTERVAL 1 YEAR))
);
这里,在注册时,MySQL 会将 reg_date The value of the column is set to the current date.According to the registration date,The due date will be calculated.Let's try to insert the value and check if the due date column is getting the correct value.为此,We will explicitly insert some registration dates.
INSERT INTO members(name) VALUES ("Bob"),("Vilas"),("John");
INSERT INTO members(reg_date,name) VALUES ('2022-08-01',"Adam"),('2022-08-04',"Cetty");
Let's check the output.
SELECT * FROM members;
interval instance
如您所见,The expiration date is set after the registration date 1 年.
概括
在本教程中,我们了解了 interval 关键字及其值.注意,The range is wide.You can use it for different purposes,For example lookups will be in the future 10 Students who complete the course within days、A list of members whose membership will expire next month, etc.练习!
边栏推荐
- MySQL INTERVAL 关键字指南
- Slider/Carousel图片切换支持触摸屏
- 美国弗吉尼亚大学、微软 | Active Data Pattern Extraction Attacks on Generative Language Models(对生成语言模型的主动数据模式提取攻击)
- VIM实用指南(-1)VIM的前世今生
- LeetCode50天刷题计划(Day 7—— 字符串转换整数 (atoi) 12.20-15.20)
- MySQL data processing of authorization 】 【
- 软测面试如何介绍项目?要做哪些技术准备?
- Kubernetes 进阶训练营 控制器
- 华盛顿大学、Allen AI 等联合 | RealTime QA: What's the Answer Right Now?(实时 QA:现在的答案是什么?)
- 泰国 好产品推荐!2022年最好的胶原蛋白评测有哪些? 喝出健康和美丽适合需要改善肌肤
猜你喜欢
MySQL [create and manage tables]
Ant discloses the open source layout of core basic software technology for the first time
hzero-resource秒退
如何有效地开发 Jmix 扩展组件
MySQL【创建和管理表】
Kernel pwn 入门 (6)
LeetCode50天刷题计划(Day 8—— 盛最多水的容器(23.00-1.20)
Shell basic function writing
Meeting OA project (6) --- (to-be-opened meeting, historical meeting, all meetings)
canvas粒子雨动画js特效
随机推荐
【repo】SyntaxError: invalid syntax
华盛顿大学、Allen AI 等联合 | RealTime QA: What's the Answer Right Now?(实时 QA:现在的答案是什么?)
Convert tensor to image in pytorch
测试工程师进阶必读书目
nodejs安装淘宝镜像(配置淘宝镜像)
LeetCode50天刷题计划(Day 6—— 整数反转 14.20-15.20)
MySQL【数据处理的增删改】
指针进阶(三)之指针与数组笔试题
kubelet node pressure eviction
SQL函数 TIMESTAMPDIFF
pytorch测试的时候为何要加上model.eval()?
gconf/dconf实战编程(3)利用dconf库读写配置实战以及诸多配套工具演示
第十三章 手动创建 REST 服务(一)
OneFlow源码解析:Op、Kernel与解释器
预定义和自定义
到底什么才是真正的商业智能(BI)
便携烙铁开源系统IronOS,支持多款便携DC, QC, PD供电烙铁,支持所有智能烙铁标准功能
全网最全音视频媒体流
Go unit tests
PHP security flaws: session hijacking, cross-site scripting, SQL injection and how to fix them