Get the App
SLTechnology News&Howtos  ›  Database  › 

How to get the day before and the day after the current time in MySQL

Shulou Source: shulou.com Published: 2022-05-31 21:02:16 09月28日 Update

This article introduces how to get the current time in MySQL the day before and the day after. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

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)

About how to get the current time in MySQL the day before and the day after sharing here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Plus one time hour subtlety three content function date week more help good interest guy buddy article knowledge article reference experiment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia vpn Redmi MySQL