我有一个包含这些值的Birthdate字段:
28 Juillet 1982 (I want this one to be excluded)
09/16/1981 (This one will be used)
(empty, should be excluded)
03/19/1953 (This one will be used)
我想获得 mm/dd/yyyy 日期的平均年龄。现在我的查询是:
SELECT AVG(NULLIF(YEAR(NOW())-YEAR(Birthdate), 0)) AS AvgAge FROM `User`
我也试过:
SELECT AVG( YEAR(NOW())-YEAR(Birthdate) ) AS AvgAge FROM `User`
但结果总是一样的:NULL
每个 100% 数字的 id_user 都有正确的 mm/dd/yyyy 生日,所以我也尝试过:
SELECT AVG(CASE WHEN id_user >=0 THEN YEAR( NOW( ) ) - YEAR( Birthdate ) ELSE NULL END ) AS AvgAge FROM `User`
然后:
SELECT AVG(CASE WHEN CEIL(id_user) = id_user THEN YEAR( NOW( ) ) - YEAR( Birthdate ) ELSE NULL END ) AS AvgAge FROM `User`
仍然为空...
你知道为什么会这样吗?
也许是我的一个简单错误,但我无法弄清楚。
谢谢 !