Jump to content

Using mySQL SUM...


Noob2Sql

Recommended Posts

Hello I am working on a script that will be able to add up a points column in my database and order it by the User_id based on how many points they have. I am new to the whole mysql scene so please bare with me.

 

Ok here is my code I am using in my PHP file:

 

$query = "SELECT race_session, SUM(points) AS totalpoints FROM $race_session GROUP BY user_id";
$rs = mysql_query($query) or die(mysql_error());
while($r = mysql_fetch_assoc($rs))
{ echo "SUM: {$r['totalpoints']}"; }

 

My database schema for this table is:

 

-- 
-- Table structure for table `session_race`
-- 

CREATE TABLE `session_race` (
  `id_race` bigint(15) unsigned NOT NULL auto_increment,
  `race_id` bigint(10) unsigned NOT NULL default '0',
  `user_id` bigint(15) unsigned NOT NULL default '0',
  `driver` varchar(255) NOT NULL,
  `f` mediumint(5) unsigned NOT NULL default '0',
  `s` mediumint(5) unsigned NOT NULL default '0',
  `race_interval` varchar(255) NOT NULL,
  `race_laps` smallint(3) unsigned NOT NULL default '0',
  `led` varchar(50) NOT NULL,
  `points` mediumint(5) unsigned NOT NULL default '0',
  `totalpoints` varchar(45) NOT NULL,
  `race_status` varchar(50) NOT NULL,
  PRIMARY KEY  (`id_race`),
  KEY `points` (`points`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;

 

I am to my wits end about what to do to fix this, somebody please help me.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.