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

How to use TIMESTAMPDIFF and DATEDIFF in MySQL

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

How are TIMESTAMPDIFF and DATEDIFF used in MySQL? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

The usage of time difference function TIMESTAMPDIFF and DATEDIFF

We frequently use the comparison and judgment of date and time in writing sql statements, especially in stored procedures, so give an example of the usage of these two time difference comparison functions.

Datediff function. The return value is the number of days of difference, and cannot be located to hours, minutes, and seconds.

-- A difference of 2 days select datediff ('2018-03-22 09, 2018-03-20 07)

TIMESTAMPDIFF function, with parameters set, can be accurate to days (DAY), hours (HOUR), minutes (MINUTE) and seconds (SECOND). It is more flexible to use than the datediff function. For the comparison of the two times, the small time is put in front and the big time is put in the back.

-- A difference of one day select TIMESTAMPDIFF (DAY, '2018-03-20 23559,' 2015-03-2200);-- A difference of 49 hours (HOUR, '2018-03-2009);-- A difference of 2940 minutes (select TIMESTAMPDIFF) (MINUTE,' 2018-03-2009, 0000, 2018-03-22). -- A difference of 176400 seconds between select TIMESTAMPDIFF (SECOND, '2018-03-20 09, 00-00,' 2018-03-22 10-00-00')

How to write in a stored procedure:

Note: var_committime is the time that needs to be compared in the previous business.

-- get the current time SET var_current_time = CONCAT (CURDATE (),'', CURTIME ());-- time comparison SET var_time_diff = TIMESTAMPDIFF (MINUTE, var_committime, var_current_time);-- determine whether the unreviewed contract is unprocessed for more than 48 hours. If it exceeds, it will be processed logically, otherwise it will not be processed. IF (var_time_diff > 2880) THEN-- related business logic processing END IF; 's questions about how to use TIMESTAMPDIFF and DATEDIFF in MySQL are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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