从出生日期计算年龄与年龄之间的条件

时间:2021-03-01 10:17:11

标签: mysql

我像这样使用 MySQL 计算年龄

A   12-10-2020  Plan A
A   12-10-2020  Plan B
A   12-10-2020  Plan C
B   16-10-2020  Plan A
C   25-10-2020  Plan A
C   25-10-2020  Plan B
D   27-10-2020  Plan A
D   27-10-2020  Plan C
E   29-10-2020  Plan A

但我想获取年龄和 MySQL 之间的数据,显示“年龄”的未知列,那么我该如何设置条件?

1 个答案:

答案 0 :(得分:1)

变体 1(表达式代替输出名称):

SELECT timestampdiff(year, md.birthDate, curdate()) as age 
from user md
WHERE timestampdiff(year, md.birthDate, curdate()) BETWEEN 10 and 20

变体 2(HAVING 而不是 WHERE):

SELECT timestampdiff(year, md.birthDate, curdate()) as age 
from user md
HAVING age BETWEEN 10 and 20