Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Commands for Mysql date calculation

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report