当前位置:网站首页>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.练习!
边栏推荐
猜你喜欢
随机推荐
显示为弹出窗口是什么意思(电脑总是弹出广告)
Chapter 13 Manually create a REST service (1)
pynlpir更新license Error: unable to fetch newest license解决方案
链滴的几个 Markdown 语法没有渲染
MySQL查询上的问题
泰国 好产品推荐!2022年最好的胶原蛋白评测有哪些? 喝出健康和美丽适合需要改善肌肤
选择合适的 DevOps 工具,从理解 DevOps 开始
BPM是什么意思?BPM的优势及好处有哪些?
ECCV 2022 | Poseur:你以为我是姿态估计,其实是目标检测哒
2.8K 120Hz触控双屏加持 灵耀X 双屏Pro 2022让办公无惧想象
探讨if...else的替代方案
中国驻西班牙使馆再次提醒留学人员注意暑期安全
蚂蚁首次披露核心基础软件技术开源版图
Go 单元测试
gconf/dconf实战编程(3)利用dconf库读写配置实战以及诸多配套工具演示
ESP8266-Arduino编程实例-GA1A12S202对数刻度模拟光传感器
hzero-resource秒退
经验|如何做好业务测试?
VIM实用指南(3)复制,粘贴 ,删除,撤销,重做指令速记
1个月写900多条用例,2线城市年薪33W+的测试经理能有多卷?









