Get the App
SLTechnology News&Howtos  ›  Database  › 

Commands for Mysql date calculation

Shulou Source: shulou.com Published: 2022-05-31 18:23:07 09月10日 Update

This article mainly introduces "the command of Mysql date calculation". In the daily operation, I believe that many people have doubts about the command of Mysql date calculation. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Mysql date calculation command". Next, please follow the editor to study!

MySQL adds a time interval to the date with the date_add () function

MySQL subtracts a time interval from the date using the date_sub () function

The experiments are as follows:

Mysql > select now ()

+-+

| | now () |

+-+

| | 2017-03-15 16:57:30 |

+-+

1 row in set (0.00 sec)

Mysql > set @ dt = now ()

Query OK, 0 rows affected (0.00 sec)

Add one day:

Mysql > select date_add (@ dt, interval 1 day)

+-+

| | date_add (@ dt, interval 1 day) | |

+-+

| | 2017-03-16 16:57:32 |

+-+

1 row in set (0.00 sec)

One day less:

Mysql > select date_sub (@ dt, interval 1 day)

+-+

| | date_sub (@ dt, interval 1 day) | |

+-+

| | 2017-03-14 16:57:32 |

+-+

1 row in set (0.00 sec)

By the same token, an additional hour and a minute can be used:

Select date_add (@ dt, interval 1 hour);-- add one hour

Select date_add (@ dt, interval 1 minute);-- add one minute

Select date_add (@ dt, interval 1 second);-- add one second

Select date_add (@ dt, interval 1 microsecond);-- add a nuance

Select date_add (@ dt, interval 1 week);-- add one week

Select date_add (@ dt, interval 1 month);-- add one month

Select date_add (@ dt, interval 1 quarter); add one quarter (three months)

One hour less, one minute can be used:

Select date_sub (@ dt, interval 1 hour);-- one hour less

Select date_sub (@ dt, interval 1 minute);-- one minute less

Select date_sub (@ dt, interval 1 second);-- one second less

Select date_sub (@ dt, interval 1 microsecond);-- reduce a little bit

Select date_sub (@ dt, interval 1 week);-- one week less

Select date_sub (@ dt, interval 1 month);-- one month less

Select date_sub (@ dt, interval 1 quarter);-reduced by one quarter (three months)

At this point, the study of "Mysql date calculation command" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Plus one date command hour learning subtle three function time week more help practical next article method theory knowledge article website Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information NVidia MySQL Shulou Technology