vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Sum of values based on usergroupid, using two tables (https://vborg.vbsupport.ru/showthread.php?t=135047)

brownafroduck 12-29-2006 04:47 PM

Sum of values based on usergroupid, using two tables
 
I'm a webmaster relatively inexperienced with databases and mySQL, so I am looking for any sort of pointer in the right direction like this. I've attempted to write a few SUM statements (is that even what you call them? statements?) using resources around the web, but I just can't seem to get this right.

I have two tables user and userfield.

In the user table I have the userid and usergroupid values.
In the userfield table I have the userid and userfield16 (an integer value representing points) values.

I would like to total the userfield16 points based on the usergroupid and have this value displayed on the forumhome index.php.

Make sense?

Paul M 12-29-2006 06:09 PM

This should do you for the sql;

[sql]
SELECT usergroupid, SUM(userfield16) AS total FROM user
LEFT JOIN userfield USING (userid)
GROUP BY usergroupid
[/sql]

brownafroduck 12-29-2006 07:44 PM

Thanks for the quick reply!

I feel like I'm doing this completely incorrectly. This query gives me an array? Or what? I'm a complete noob at this, so here is my feeble attempt. I used the SQL that you gave me...

PHP Code:

$resultmysql_query("
                SELECT usergroupid, SUM(userfield16) AS total FROM " 
TABLE_PREFIX "user
                LEFT JOIN " 
TABLE_PREFIX "userfield USING (userid)
                GROUP BY usergroupid
                "
) or die(mysql_error());
                
$points mysql_fetch_array($result);

$group1$points[0];
$group2$points[1];
$group3$points[2];
$group4$points[3]; 

But I get this error...
Code:

Unknown column 'userfield16' in 'field list'
What am I doing wrong? What do I need to change? If I know that the usergroupid values that I need to sum up the points are 11, 12, 13, and 14, does that make it any easier? Is there perhaps some other approach that I can take to this with that knowledge?

Thanks!


All times are GMT. The time now is 07:00 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.01005 seconds
  • Memory Usage 1,720KB
  • 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
  • (1)bbcode_code_printable
  • (1)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (3)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