vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Query question - PLEASE HELP!!! (https://vborg.vbsupport.ru/showthread.php?t=99273)

Lea Verou 10-25-2005 10:06 AM

Query question - PLEASE HELP!!!
 
I really need a hack that displays the average age. Currently I am using this plugin:

PHP Code:

$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:

[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 " . TABLE_PREFIX . "user WHERE YEAR(birthday_search)>0
[/sql]

but it gives a database error:
Code:

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:

Xenon 10-25-2005 10:31 AM

try that query

[sql]
SELECT ROUND(AVG(YEAR(NOW()) - YEAR(birthday_search) - (DAYOFYEAR(birthday_search) - DAYOFYEAR(NOW())) / 365 AS avgage
FROM testvb_user
WHERE YEAR(birthday_search)>0;[/sql]

Lea Verou 10-25-2005 10:42 AM

Thanks so much but I still get a db error... :(

Database error in vBulletin 3.5.0:
Code:

Invalid SQL:
SELECT ROUND(AVG(YEAR(NOW()) - YEAR(birthday_search) - (DAYOFYEAR(birthday_search) - DAYOFYEAR(NOW())) / 365 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 'AS avgage
FROM testvb_user
WHERE YEAR(birthday_search)>0' a
Error Number : 1064

This is how I put it in the plugin:
PHP Code:

$age_result=$db->query_first("SELECT ROUND(AVG(YEAR(NOW()) - YEAR(birthday_search) - (DAYOFYEAR(birthday_search) - DAYOFYEAR(NOW())) / 365 AS avgage 
FROM testvb_user 
WHERE YEAR(birthday_search)>0;"
);
$avg_age=$age_result['avgage']; 


Marco van Herwaarden 10-25-2005 10:58 AM

Add a ')' in front of the AS avgage.

Lea Verou 10-25-2005 11:14 AM

Code:

Database error in vBulletin 3.5.0:

Invalid SQL:
SELECT ROUND(AVG(YEAR(NOW()) - YEAR(birthday_search) - (DAYOFYEAR(birthday_search) - DAYOFYEAR(NOW())) / 365) 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 'AS avgage
FROM testvb_user
WHERE YEAR(birthday_search)>0' a
Error Number : 1064

:( :( :(
I have started to think that it will never work out... :( :( :(

Xenon 10-25-2005 11:25 AM

it's just a problem with brackets ;)

[sql]SELECT ROUND(AVG(YEAR(NOW()) - YEAR(birthday_search) - (DAYOFYEAR(birthday_search) - DAYOFYEAR(NOW()) / 365))) AS avgage
FROM testvb_user
WHERE YEAR(birthday_search)>0;[/sql]

Lea Verou 10-25-2005 11:29 AM

Now there is no database error (woo hoo!! :D) but...
Average Age: -150 :(

Xenon 10-25-2005 12:30 PM

very young users you have ;)

my fault, try that:

[sql]SELECT ROUND(AVG(YEAR(NOW()) - YEAR(birthday_search) - (DAYOFYEAR(birthday_search) - DAYOFYEAR(NOW())) / 365)) AS avgage
FROM testvb_user
WHERE YEAR(birthday_search)>0;[/sql]

Lea Verou 10-25-2005 12:36 PM

It looks correct, thanks so much!!!!!! :D :D :D

Another question, not so urgent, answer if and when you have time... How can I make it to display 2 decimals? :)

Thanks again!

Also, do you allow me to release this as a modification? Others may need it too. Of course, I'll include the appropriate credits :)

Xenon 10-25-2005 12:42 PM

glad we could solve it :)

2 decimals are very easy:

[sql]SELECT ROUND(AVG(YEAR(NOW()) - YEAR(birthday_search) - (DAYOFYEAR(birthday_search) - DAYOFYEAR(NOW())) / 365), 2) AS avgage
FROM testvb_user
WHERE YEAR(birthday_search)>0;[/sql]

feel free to release it

Lea Verou 10-25-2005 12:54 PM

Thanks a lot!!!!
I'll release it but it will be quite messy as the user will have to manually change the plugin to enter his db prefix, if I try to put the variable TABLE_PREFIX it gives a db error for no reason. Really weird! :)

Marco van Herwaarden 10-26-2005 09:34 AM

What is the exact php code you are using to run this query?

Lea Verou 10-26-2005 09:46 AM

Quote:

Originally Posted by MarcoH64
What is the exact php code you are using to run this query?

Look at the first message, I am using the same code with a different query ;)

Marco van Herwaarden 10-26-2005 12:18 PM

That should not give a SQL error on the TABLE_PREFIX. What is the exact error?

Lea Verou 10-26-2005 12:33 PM

Nevermind, it's ok now, tamarian fixed it :)


All times are GMT. The time now is 11:40 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02076 seconds
  • Memory Usage 1,752KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (2)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (15)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete