语法
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;
运行结果