0

我有一个有 3 列的表。Name, DateOfBirth, DateDeceded. 字段DateOfBirthDateDeceded具有数据类型DATE

现在当我做一个Select * from TableName. 我想显示姓名、出生日期、过世日期和该人居住的年数(DateDeceded- DateOfBirth)。

如何编写 SQL Select 语句以包含该NumberOfYearsLived字段?

4

1 回答 1

4

尝试:

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

于 2012-07-28T19:18:39.960 回答