我有一个包含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
关于mysql - SELECT语句-添加另一个派生字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11703885/