如何在 MySQL 上获取两个日期之间相差的天数?

How to get the number of days of difference between two dates on MySQL?(如何在 MySQL 上获取两个日期之间相差的天数?)
本文介绍了如何在 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.

推荐答案

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 上获取两个日期之间相差的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)