我像这样使用 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 之间的数据,显示“年龄”的未知列,那么我该如何设置条件?
答案 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