Noob2Sql Posted June 8, 2009 Share Posted June 8, 2009 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. Quote Link to comment Share on other sites More sharing options...
Noob2Sql Posted June 8, 2009 Author Share Posted June 8, 2009 If anybody needs any more info about how all this works please let me know as this is driving me nuts right now lol. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 9, 2009 Share Posted June 9, 2009 Add an ORDER BY totalpoints. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.