当前位置:网站首页>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.练习!
边栏推荐
- eslint语法报错解决
- LeetCode50天刷题计划(Day 6—— 整数反转 14.20-15.20)
- 【repo】SyntaxError: invalid syntax
- 七夕专属博文-使用QGraphics画“红心“或“黑心“(含数学模型讲解)
- MySQL data processing of authorization 】 【
- PHP security flaws: session hijacking, cross-site scripting, SQL injection and how to fix them
- js判断是pc端还是移动端(包括ipad)
- CodeForces 570D Tree Requests
- 请问下怎么取数据库中上一个小时的数据到odps进行实时节点的同步呢
- LeetCode50天刷题计划(Day 9—— 整数转罗马数字(20.40-22.10)
猜你喜欢
随机推荐
华盛顿大学、Allen AI 等联合 | RealTime QA: What's the Answer Right Now?(实时 QA:现在的答案是什么?)
实习日报-2022-7-30
重庆银河证券股票开户安全吗,是正规的证券公司吗
gconf/dconf实战编程(3)利用dconf库读写配置实战以及诸多配套工具演示
Spark: Cluster Computing with Working Sets
Spark: Cluster Computing with Working Sets
MySQL [create and manage tables]
gconf/dconf实战编程(2)利用gconf库读写配置实战以及诸多配套工具演示
1个月写900多条用例,2线城市年薪33W+的测试经理能有多卷?
【Untitled】
Use Canvas to implement mobile phone signature
给网站增加离开页面改变网站标题效果
如何防止重复下单?
Can MySQL do two-way synchronization of multiple vps?
指针进阶(二)
LeetCode50天刷题计划(Day 6—— 整数反转 14.20-15.20)
强网杯2022 pwn 赛题解析——yakagame
搭建云计算平台(云计算管理平台搭建)
提速!进口婴幼儿配方产品出证仅需1-3天
蚂蚁首次披露核心基础软件技术开源版图









