DanDaBeginner Posted April 17, 2007 Share Posted April 17, 2007 what I did is to get the $fromAge and $toAge, I did this with PHP..compute the year then insert to query..ex. $tAge = date("Y") - 18; //1989 $fAge = date("Y") - 22; //1985 $query = " SELECT * from dating_profile WHERE EXTRACT(YEAR FROM birthdate) BETWEEN $fAge and $tAge " and it works fine.. im just wondering how to do it in mysql query instead of computing the $tAge and $fAge.. if I will do this in mysql query will it be faster than computing 1st the $tAge and $fAge then query? thanx Dan Quote Link to comment https://forums.phpfreaks.com/topic/47374-solved-how-to-query-age-between-18yo-and-22yo/ Share on other sites More sharing options...
bubblegum.anarchy Posted April 17, 2007 Share Posted April 17, 2007 I am actually not sure what you are asking but this should calculate the age based on a birthdate SELECT round(datediff(CURRENT_DATE, birthdate) / 365) AS age FROM dating_profile; Quote Link to comment https://forums.phpfreaks.com/topic/47374-solved-how-to-query-age-between-18yo-and-22yo/#findComment-231104 Share on other sites More sharing options...
DanDaBeginner Posted April 17, 2007 Author Share Posted April 17, 2007 thanx bubblegum.anarchy! I actually want to get the row, age from 18 to 22... Quote Link to comment https://forums.phpfreaks.com/topic/47374-solved-how-to-query-age-between-18yo-and-22yo/#findComment-231108 Share on other sites More sharing options...
bubblegum.anarchy Posted April 17, 2007 Share Posted April 17, 2007 oh ok - that is the part I misunderstood.. so you will still need WHERE round(datediff(CURRENT_DATE, birthdate) / 365) BETWEEN $fAge AND $tAge Quote Link to comment https://forums.phpfreaks.com/topic/47374-solved-how-to-query-age-between-18yo-and-22yo/#findComment-231127 Share on other sites More sharing options...
DanDaBeginner Posted April 18, 2007 Author Share Posted April 18, 2007 thanx again bro, youve been a great help to me..would you think it is slower than the one I have? because it seems to me that it is slower than the one I have because it will still need to do some arithmetic before processing the search.. any more suggestion..? Quote Link to comment https://forums.phpfreaks.com/topic/47374-solved-how-to-query-age-between-18yo-and-22yo/#findComment-231874 Share on other sites More sharing options...
bubblegum.anarchy Posted April 18, 2007 Share Posted April 18, 2007 I suppose extract(year from birthdate) would be faster than round(datediff(CURRENT_DATE, birthdate) / 365) and if the following is correct than extracting the date is substantially quicker: DROP TABLE IF EXISTS birthdate; CREATE TABLE birthdate ( id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, birthdate DATE NOT NULL )TYPE=MYISAM; INSERT INTO birthdate SET birthdate = '1974-12-03'; INSERT INTO birthdate (birthdate) SELECT adddate(birthdate, 1) FROM birthdate; # repeated a number of times SELECT count(*) FROM birthdate; # records created 2097152 SELECT count(round(datediff(CURRENT_DATE, birthdate) / 365)) AS test FROM birthdate; # average around 922 ms SELECT count(extract(year from birthdate)) AS test FROM birthdate; # average around 328 ms Quote Link to comment https://forums.phpfreaks.com/topic/47374-solved-how-to-query-age-between-18yo-and-22yo/#findComment-231907 Share on other sites More sharing options...
DanDaBeginner Posted April 18, 2007 Author Share Posted April 18, 2007 cool, thanx, so I will stick to my current code... Quote Link to comment https://forums.phpfreaks.com/topic/47374-solved-how-to-query-age-between-18yo-and-22yo/#findComment-231931 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.