如何计算再次年龄状况的年龄?我希望所有用户只有年龄< 50年
我的查询在没有条件的情况下工作正常,但当我添加WHERE age < 50
时,它会给我一个错误#1054 - Unknown column 'age' in 'where clause'
SELECT u.*, YEAR(CURRENT_TIMESTAMP) - YEAR(u.user_dob) - (RIGHT(CURRENT_TIMESTAMP, 5) <
RIGHT(u.user_dob, 5)) as age FROM tbl_users as u WHERE age < 50
答案 0 :(得分:2)
这样的事情怎么样?
where dob >= date_sub(curdate(), interval 50 year)
如果你想要这个年龄,你可以采用多年的差异:
select timestampdiff(year, dob, curdate())
答案 1 :(得分:1)
用where:
更新where子句中的年龄YEAR(CURRENT_TIMESTAMP) - YEAR(u.user_dob) - (
RIGHT(CURRENT_TIMESTAMP, 5) <
RIGHT(u.user_dob, 5))
是:
SELECT u.*, YEAR(CURRENT_TIMESTAMP) - YEAR(u.user_dob) - (
RIGHT(CURRENT_TIMESTAMP, 5) <
RIGHT(u.user_dob, 5)) AS age
FROM tbl_users AS u
WHERE YEAR(CURRENT_TIMESTAMP) - YEAR(u.user_dob) - (
RIGHT(CURRENT_TIMESTAMP, 5) <
RIGHT(u.user_dob, 5)) < 50