mysql中怎么根据生日(brithday)获取年龄

语法

YEAR(from_days(datediff(now(), sbirthday)));

举例

首先先创建一个表

create table studentone(
sname varchar(20) not null,
sbirthday datetime
)charset=utf8;

再插入数据

insert into studentone values('陈奇','2000-11-02');
insert into studentone values('陈飞','1999-12-05');
insert into studentone values('周杰','1989-03-01');
insert into studentone values('昆凌','1989-04-15');
insert into studentone values('王丽','1999-05-16');
insert into studentone values('蔡蔡','2000-08-17');

查看表
在这里插入图片描述
要求:将studentone表里面的sname及其年龄查询出来
代码

SELECT sname,YEAR(from_days(datediff(now(), sbirthday)))
FROM studentone;

运行结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/hanhanwanghaha/article/details/106316920