如何获取mysql两个日期之间的差异天数? [英] How to get the number of days of difference between two dates on mysql?

查看:328
本文介绍了如何获取mysql两个日期之间的差异天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在mysql中获取几个日期中包含的天数。

I need to get the number of days contained within a couple of dates on mysql.

例如:入住日期12-04-2010退房日期15-04-2010和日差异将为3

For example: Check in date 12-04-2010 Check out date 15-04-2010 and the day difference would be 3

推荐答案

DATEDIFF 功能?

What about the DATEDIFF function ?

引用手册的页面:


DATEDIFF()返回expr1 - expr2
表示为从一个
日期到另一个
日期的值。 expr1和expr2
是日期或日期和时间表达式。
只有值的日期部分是
在计算中使用

DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation



在你的情况下,你可以使用:


In your case, you'd use :

mysql> select datediff('2010-04-15', '2010-04-12');
+--------------------------------------+
| datediff('2010-04-15', '2010-04-12') |
+--------------------------------------+
|                                    3 | 
+--------------------------------------+
1 row in set (0,00 sec)

但请注意日期应写成 YYYY-MM-DD ,而不是 DD-MM-YYYY 像您发布的那样。

But note the dates should be written as YYYY-MM-DD, and not DD-MM-YYYY like you posted.

这篇关于如何获取mysql两个日期之间的差异天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆