How to calculate age with where condition on age again ? I want all users only of age < 50 year
My query is working fine without where condition but when i am adding WHERE age < 50
it's give me an error #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
How about something simpler like this?
where dob >= date_sub(curdate(), interval 50 year)
If you want the age, you can take the difference in years:
select timestampdiff(year, dob, curdate())
update your age in where clause with this:
YEAR(CURRENT_TIMESTAMP) - YEAR(u.user_dob) - (
RIGHT(CURRENT_TIMESTAMP, 5) <
RIGHT(u.user_dob, 5))
to be:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.