在MySQL中计算年龄(InnoDb)

如果我将一个人的出生date以dd-mm-yyyy的forms存储在表格中,并且从当前date中减去它,那么返回的date格式是什么?

我怎样才能用这个返回的格式来计算某个人的年龄?

如果该值存储为DATETIME数据types:

 SELECT YEAR(CURRENT_TIMESTAMP) - YEAR(dob) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(dob, 5)) as age FROM YOUR_TABLE 

当你考虑闰年时不太确切:

 SELECT DATEDIFF(CURRENT_DATE, STR_TO_DATE(t.birthday, '%d-%m-%Y'))/365 AS ageInYears FROM YOUR_TABLE t 

您可以使用TIMESTAMPDIFF(unit, datetime_expr1, datetime_expr2)函数:

 SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age 

演示

 select *,year(curdate())-year(dob) - (right(curdate(),5) < right(dob,5)) as age from your_table 

以这种方式,你甚至可以考虑出生的月份和date,以便有一个更准确的年龄计算。

 SELECT TIMESTAMPDIFF (YEAR, YOUR_COLUMN, CURDATE()) FROM YOUR_TABLE AS AGE 

点击进行演示

简单但优雅..

 select floor(datediff (now(), birthday)/365) as age 

只是:

 DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(`birthDate`)), '%Y')+0 AS age 

尝试这个:

 SET @birthday = CAST('1980-05-01' AS DATE); SET @today = CURRENT_DATE(); SELECT YEAR(@today) - YEAR(@birthday) - (CASE WHEN MONTH(@birthday) > MONTH(@today) OR (MONTH(@birthday) = MONTH(@today) AND DAY(@birthday) > DAY(@today)) THEN 1 ELSE 0 END); 

今年 – 出生年度(生日之后今年是多less岁),并且根据今年是否有过生日的情况调整。

它不会受到这里介绍的其他方法的舍入误差。

从这里自由改编

这是如何计算MySQL中的年龄:

 select date_format(now(), '%Y') - date_format(date_of_birth, '%Y') - (date_format(now(), '00-%m-%d') < date_format(date_of_birth, '00-%m-%d')) as age from table 

你可以做一个function来做到这一点:

 drop function if exists getIdade; delimiter | create function getIdade( data_nascimento datetime ) returns int begin declare idade int; declare ano_atual int; declare mes_atual int; declare dia_atual int; declare ano int; declare mes int; declare dia int; set ano_atual = year(curdate()); set mes_atual = month( curdate()); set dia_atual = day( curdate()); set ano = year( data_nascimento ); set mes = month( data_nascimento ); set dia = day( data_nascimento ); set idade = ano_atual - ano; if( mes > mes_atual ) then set idade = idade - 1; end if; if( mes = mes_atual and dia > dia_atual ) then set idade = idade - 1; end if; return idade; end| delimiter ; 

现在,你可以从一个date的年龄:

 select getIdade('1983-09-16'); 

如果你的date格式是Ymd H:i:s,你可以这样做:

 select getIdade(substring_index('1983-09-16 23:43:01', ' ', 1)); 

你可以在任何地方重用这个function;)

我更喜欢用这种方式来使用函数。

 DELIMITER $$ DROP FUNCTION IF EXISTS `db`.`F_AGE` $$ CREATE FUNCTION `F_AGE`(in_dob datetime) RETURNS int(11) NO SQL BEGIN DECLARE l_age INT; IF DATE_FORMAT(NOW( ),'00-%m-%d') >= DATE_FORMAT(in_dob,'00-%m-%d') THEN -- This person has had a birthday this year SET l_age=DATE_FORMAT(NOW( ),'%Y')-DATE_FORMAT(in_dob,'%Y'); ELSE -- Yet to have a birthday this year SET l_age=DATE_FORMAT(NOW( ),'%Y')-DATE_FORMAT(in_dob,'%Y')-1; END IF; RETURN(l_age); END $$ DELIMITER ; 

现在使用

 SELECT F_AGE('1979-02-11') AS AGE; 

要么

 SELECT F_AGE(date) AS age FROM table; 

由于这个问题被标记为mysql ,我有以下的实现对我有用,我希望类似的替代scheme将在那里的其他RDBMS的。 这里的SQL:

 select YEAR(now()) - YEAR(dob) - ( DAYOFYEAR(now()) < DAYOFYEAR(dob) ) as age from table where ... 

简单地做

 SELECT birthdate, (YEAR(CURDATE())-YEAR(birthdate)) AS age FROM `member` 

birthdate是保存birthdate名称的字段名称将CURDATE()转换为YEAR()命令减去年份()中的YEAR()

有两个简单的方法来做到这一点:

1-

 select("users.birthdate", DB::raw("FLOOR(DATEDIFF(CURRENT_DATE, STR_TO_DATE(users.birthdate, '%Y-%m-%d'))/365) AS age_way_one"), 

2-

 select("users.birthdate",DB::raw("(YEAR(CURDATE())-YEAR(users.birthdate)) AS age_way_two"))