Detailed explanation of MySQL date addition and subtraction functions

Detailed explanation of MySQL date addition and subtraction functions

1. addtime()

Add the specified number of seconds to the date

select addtime(now(),1); -- add 1 second

2. adddate()

There are two ways to use it. If you fill in a number directly in the second parameter, it will add the specified number of days to the date. If you fill in interval, it will add the specified interval time to the date.

select adddate(now(),1); -- add 1 dayselect adddate(now(), interval 1 day); -- add 1 dayselect adddate(now(), interval 1 hour); -- add 1 hourselect adddate(now(), interval 1 minute); -- add 1 minuteselect adddate(now(), interval 1 second); -- add 1 secondselect adddate(now(), interval 1 microsecond); -- add 1 millisecondselect adddate(now(), interval 1 week); -- add 1 weekselect adddate(now(), interval 1 month); -- add 1 monthselect adddate(now(), interval 1 quarter); -- add 1 quarterselect adddate(now(), interval 1 year); -- add 1 year

3. date_add()

Add a time interval to the date. This can only use the interval time as a parameter. The usage is the same as adddate()

select date_add(now(), interval 1 day); -- add 1 dayselect date_add(now(), interval 1 hour); -- add 1 hourselect date_add(now(), interval 1 minute); -- add 1 minuteselect date_add(now(), interval 1 second); -- add 1 secondselect date_add(now(), interval 1 microsecond); -- add 1 millisecondselect date_add(now(), interval 1 week); -- add 1 weekselect date_add(now(), interval 1 month); -- add 1 monthselect date_add(now(), interval 1 quarter); -- add 1 quarterselect date_add(now(), interval 1 year); -- add 1 year

4. subtime()

Subtract the specified number of seconds from the date

select subtime(now(), 1); -- minus 1 second

5. subdate()

The usage is the same as adddate() function. There are two usages. If you fill in a number directly in the second parameter, it will subtract the specified number of days from the date. If you fill in interval, it will subtract the specified interval time from the date.

select subdate(now(),1); -- subtract 1 dayselect subdate(now(), interval 1 day); -- subtract 1 dayselect subdate(now(), interval 1 hour); -- subtract 1 hourselect subdate(now(), interval 1 minute); -- subtract 1 minuteselect subdate(now(), interval 1 second); -- subtract 1 secondselect subdate(now(), interval 1 microsecond); -- subtract 1 millisecondselect subdate(now(), interval 1 week); -- subtract 1 weekselect subdate(now(), interval 1 month); -- subtract 1 monthselect subdate(now(), interval 1 quarter); -- subtract 1 quarterselect subdate(now(), interval 1 year); -- subtract 1 year

6. date_sub()

Same as date_add() function, subtract a time interval from the date. This function can only use interval time as a parameter.

select date_sub(now(), interval 1 day); -- subtract 1 dayselect date_sub(now(), interval 1 hour); -- subtract 1 hourselect date_sub(now(), interval 1 minute); -- subtract 1 minuteselect date_sub(now(), interval 1 second); -- subtract 1 secondselect date_sub(now(), interval 1 microsecond); -- subtract 1 millisecondselect date_sub(now(), interval 1 week); -- subtract 1 weekselect date_sub(now(), interval 1 month); -- subtract 1 monthselect date_sub(now(), interval 1 quarter); -- subtract 1 quarterselect date_sub(now(), interval 1 year); -- subtract 1 year

This is the end of this article about the detailed explanation of MySQL date addition and subtraction functions. For more relevant MySQL date addition and subtraction function summary content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL date and time addition and subtraction sample code

<<:  Vue simulates the shopping cart settlement function

>>:  Linux performance monitoring tool nmon installation and usage tutorial analysis

Recommend

Usage and difference of Js module packaging exports require import

Table of contents 1. Commonjs exports and require...

Linux kernel device driver virtual file system notes

/******************** * Virtual File System VFS *...

MySQL 8.0.12 installation and configuration method graphic tutorial

Record the installation and configuration method ...

SQL Optimization Tutorial: IN and RANGE Queries

Preface "High Performance MySQL" mentio...

Markup Language - Image Replacement

Click here to return to the 123WORDPRESS.COM HTML ...

Detailed tutorial on building a local idea activation server

Preface The blogger uses the idea IDE. Because th...

Vue song progress bar sample code

Note that this is not a project created by vue-cl...

Solution to MySQL startup successfully but not listening to the port

Problem Description MySQL is started successfully...

MySQL uses events to complete scheduled tasks

Events can specify the execution of SQL code once...

Two problems encountered when deploying rabbitmq with Docker

1. Background The following two problems are enco...