dweb Posted March 23, 2013 Share Posted March 23, 2013 Hi I have the following query; SELECT m.name,m.gender,m.dob, (SELECT FLOOR( DATEDIFF( 'date('Y-m-d')',m.dob ) / 365.25 )) AS age FROM members m which gets David Male 1990-08-17 22John Male 1978-02-15 35 and that all works ok what i want to do, is extend the query to grab rows when the member is between a set age but when i do WHERE age BETWEEN 30 AND 40 i get Unknown column 'age' in 'where clause' any idea why? thanks Quote Link to comment https://forums.phpfreaks.com/topic/276051-selecting-rows-with-a-certain-age/ Share on other sites More sharing options...
Solution mac_gyver Posted March 23, 2013 Solution Share Posted March 23, 2013 this is a chicken and egg age problem. the age value is calculated in the select term for the rows that have been selected. the where term determines which rows to select. the easiest, not fastest, way would be to use a HAVING age BETWEEN 30 AND 40 term. the fastest way would be to put the calculation into the WHERE term. Quote Link to comment https://forums.phpfreaks.com/topic/276051-selecting-rows-with-a-certain-age/#findComment-1420517 Share on other sites More sharing options...
dweb Posted March 23, 2013 Author Share Posted March 23, 2013 thank you, works great Quote Link to comment https://forums.phpfreaks.com/topic/276051-selecting-rows-with-a-certain-age/#findComment-1420518 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.