当前位置:网站首页>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.练习!
边栏推荐
- 接口测试框架开发实践5:配置文件读取
- 搭建云计算平台(云计算管理平台搭建)
- MUI as a mobile phone to return to the action bar
- VIM实用指南(3)复制,粘贴 ,删除,撤销,重做指令速记
- 引用js报错$ is not defined解决办法
- c语言rand函数生成随机数,详解C语言生成随机数rand函数的用法[通俗易懂]
- 蚂蚁首次披露核心基础软件技术开源版图
- IronOS, an open source system for portable soldering irons, supports a variety of portable DC, QC, PD powered soldering irons, and supports all standard functions of smart soldering irons
- pwnhome 个人博客快速索引(持续更新)
- 30分钟成为Contributor|如何多方位参与OpenHarmony开源贡献?
猜你喜欢

Spark: Cluster Computing with Working Sets

IronOS, an open source system for portable soldering irons, supports a variety of portable DC, QC, PD powered soldering irons, and supports all standard functions of smart soldering irons

使用Canvas 实现手机端签名

Use Canvas to implement mobile phone signature

月薪12K,蝶变向新勇往直前,我通过转行软件测试实现月薪翻倍...

Kernel pwn 入门 (6)

HashCode technology insider interview must ask

js邯郸市地图网页源码下载

OneFlow源码解析:Op、Kernel与解释器

14年测试人最近的面试经历,值得借鉴√
随机推荐
PHP 安全漏洞:会话劫持、跨站点脚本、SQL 注入以及如何修复它们
Ranking of itineraries (summer vacation daily question 12)
LeetCode50天刷题计划(Day 7—— 字符串转换整数 (atoi) 12.20-15.20)
计算机系统与网络安全技术——第一章——信息安全概述——1.1-网络安全定义——什么是信息?
PAT 甲级 A1003 Emergency
显示为弹出窗口是什么意思(电脑总是弹出广告)
设计专业第一台笔记本 华硕灵耀Pro16 2022 新品首发超值入手
主流定时任务解决方案全横评
pytorch中tensor转成图片保存
CodeForces 570D Tree Requests
nodejs安装淘宝镜像(配置淘宝镜像)
珠海市生物安全P3实验室主体结构封顶
手机扫码登陆原理(扫码充电线原理)
kubelet node pressure eviction
ESP8266-Arduino编程实例-74HC595位移寄存驱动
蚂蚁首次披露核心基础软件技术开源版图
美国弗吉尼亚大学、微软 | Active Data Pattern Extraction Attacks on Generative Language Models(对生成语言模型的主动数据模式提取攻击)
pwnhome 个人博客快速索引(持续更新)
LeetCode50天刷题计划(Day 10—— 三数之和(20.50-22.40)
MySQL【数据处理的增删改】