How to get the number of days of difference between two dates on 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 上获取两个日期之间相差的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 MySQL 上获取两个日期之间相差的天数?


- 更改自动增量起始编号? 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- SQL 临时表问题 2022-01-01