如何计算MySQL中两个日期之间的差异 [英] How to calculate difference between two dates in months in MySQL
本文介绍了如何计算MySQL中两个日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在MySQL表中有两列:
I have two columns in a MySQL table:
- DateOfService(datetime)
- BirthDate (日期)
我想运行一个MySQL查询,将在几个月内提供这两个字段之间的日期差异。
I want to run a MySQL query that will provide date difference between these two fields in months.
如何在MySQL选择查询中执行此操作?
How can I do this in a MySQL select query?
谢谢。
推荐答案
这可以工作:
SELECT 12 * (YEAR(DateOfService)
- YEAR(BirthDate))
+ (MONTH(DateOfService)
- MONTH(BirthDate)) AS months
FROM table
这篇关于如何计算MySQL中两个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文