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
https://forums.phpfreaks.com/topic/161428-using-mysql-sum/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.