gpt4 book ai didi

mysql - MySQL 中 DATE_ADD/INTERVAL 的反转

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:05 25 4
gpt4 key购买 nike

在mysql中,我们可以做如下添加月份:

SELECT DATE_ADD('2014-08-20', INTERVAL 13 MONTH); //Result: 2015-09-20

有什么办法可以进行反向操作吗?示例:

SELECT DIFF_IN_MONTHS('2015-09-20', '2014-08-20') //Result: 13

由于日差造成的四舍五入对我来说不是问题。

最佳答案

函数TIMESTAMPDIFF这样做:

SELECT TIMESTAMPDIFF(MONTH, '2015-09-20', '2014-08-20');

TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)

Returns datetime_expr2 – datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions. One expression may be a date and the other a datetime; a date value is treated as a datetime having the time part '00:00:00' where necessary. The unit for the result (an integer) is given by the unit argument

关于mysql - MySQL 中 DATE_ADD/INTERVAL 的反转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25409556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com