Lea Verou
10-25-2005, 11:06 AM
I really need a hack that displays the average age. Currently I am using this plugin:
$age_result=$db->query_first("SELECT ROUND(YEAR(NOW())-AVG(YEAR(birthday_search)),0) AS avgage FROM " . TABLE_PREFIX . "user WHERE YEAR(birthday_search) <> 0;");
$avg_age=$age_result['avgage'];
but as you can see, it's not accurate as it only counts the year of the birthday and the current year...
Andreas/KirbyDE had given me this query instead that is supposed to be more accurate:
SELECT ROUND(AVG(YEAR(NOW())-YEAR(birthday_search) - (MONTH(NOW()) < MONTH(birthday_search)) - (MONTH(NOW()) = MONTH(birthday_search) AND DAY(NOW()) < DAY(birthday_search)))) AS avgage FROM " . TABLE_PREFIX . "user WHERE YEAR(birthday_search)>0
but it gives a database error:
Database error in vBulletin 3.5.0:
Invalid SQL:
SELECT ROUND(AVG(YEAR(NOW())-YEAR(birthday_search) - (MONTH(NOW()) < MONTH(birthday_search)) - (MONTH(NOW()) = MONTH(birthday_search) AND DAY(NOW()) < DAY(birthday_search)))) AS avgage FROM testvb_user WHERE YEAR(birthday_search)>0;
MySQL Error : You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(NOW()) < DAY(birthday_search)))) AS avgage FROM testvb_user WH
Error Number : 1064
Any ideas please? :nervous:
$age_result=$db->query_first("SELECT ROUND(YEAR(NOW())-AVG(YEAR(birthday_search)),0) AS avgage FROM " . TABLE_PREFIX . "user WHERE YEAR(birthday_search) <> 0;");
$avg_age=$age_result['avgage'];
but as you can see, it's not accurate as it only counts the year of the birthday and the current year...
Andreas/KirbyDE had given me this query instead that is supposed to be more accurate:
SELECT ROUND(AVG(YEAR(NOW())-YEAR(birthday_search) - (MONTH(NOW()) < MONTH(birthday_search)) - (MONTH(NOW()) = MONTH(birthday_search) AND DAY(NOW()) < DAY(birthday_search)))) AS avgage FROM " . TABLE_PREFIX . "user WHERE YEAR(birthday_search)>0
but it gives a database error:
Database error in vBulletin 3.5.0:
Invalid SQL:
SELECT ROUND(AVG(YEAR(NOW())-YEAR(birthday_search) - (MONTH(NOW()) < MONTH(birthday_search)) - (MONTH(NOW()) = MONTH(birthday_search) AND DAY(NOW()) < DAY(birthday_search)))) AS avgage FROM testvb_user WHERE YEAR(birthday_search)>0;
MySQL Error : You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(NOW()) < DAY(birthday_search)))) AS avgage FROM testvb_user WH
Error Number : 1064
Any ideas please? :nervous: