我有一个有 3 列的表。Name
, DateOfBirth
, DateDeceded
. 字段DateOfBirth
和DateDeceded
具有数据类型DATE
。
现在当我做一个Select * from TableName
. 我想显示姓名、出生日期、过世日期和该人居住的年数(DateDeceded
- DateOfBirth
)。
如何编写 SQL Select 语句以包含该NumberOfYearsLived
字段?
尝试:
SELECT Name,
DateOfBirth,
DateDeceded,
YEAR(DateDeceded) - YEAR(DateOfBirth) AS NumberOfYearsLived
FROM TableName
编辑:你可能想试试这个。可能更准确。
SELECT Name,
DateOfBirth,
DateDeceded,
TIMESTAMPDIFF(YEAR, DateDeceded, DateofBirth) AS NumberOfYearsLived
FROM TableName
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timestampdiff