3 回答
TA贡献1813条经验 获得超2个赞
我喜欢Bohemian的解决方案,但是如何使用timestampdiff
select date1, date2,timestampdiff(YEAR,date2,date1) from so7749639
似乎更容易。
TA贡献1831条经验 获得超10个赞
mysql> SELECT FLOOR(DATEDIFF('2011-06-11','2001-10-11')/365);
+------------------------------------------------+
| FLOOR(DATEDIFF('2011-06-11','2001-10-11')/365) |
+------------------------------------------------+
| 9 |
+------------------------------------------------+
1 row in set (0.00 sec)
DATEDIFF()返回两个日期之间的天数差。这并未特别考虑leap年,但在以下情况下可能会起作用:
mysql> SELECT FLOOR(DATEDIFF('2007-07-11','2004-07-11')/365);
+------------------------------------------------+
| FLOOR(DATEDIFF('2007-07-11','2004-07-11')/365) |
+------------------------------------------------+
| 3 |
+------------------------------------------------+
1 row in set (0.00 sec)
添加回答
举报