mysql中使用生日作比较

直接查询年龄段,我们表里面记录的是生日,但是我们需要根据年龄段来筛选,不需要做各种转换,mysql自带有日期处理函数。sql语句如下:

SELECT * from teacher_info where DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthday)), '%Y')+0 between 0 and 21;

更优秀的方法:

SELECT * from teacher_info where TIMESTAMPDIFF(YEAR, birthday, CURDATE()) between 0 and 22;

其他参考:

https://blog.csdn.net/Hi_Boy_/article/details/79897004

你可能感兴趣的:(mysql)