gpt4 book ai didi

mysql - 如何在MySQL中获取两个日期的月份差

转载 作者:行者123 更新时间:2023-12-01 00:17:38 26 4
gpt4 key购买 nike

如何在MySQL中取两个日期的月差。

我正在尝试获取两个日期的月份差异,但我没有。天数。

select datediff('2014-10-17T00:00:00.000-07:00', '2015-02-06T00:00:00.000-08:00'); 

最佳答案

TIMESTAMPDIFF()

是您的解决方案。


语法是

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.

~MySQL :: MySQL 5.5 Reference Manual :: 12.7 Date and Time Functions~

单位的合法值

  • MICROSECOND(微秒)
  • 第二
  • 分钟
  • 小时
  • 季度
  • 年份

例子

mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
-> 3
mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');
-> -1
mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');
-> 128885

关于mysql - 如何在MySQL中获取两个日期的月份差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51152697/

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