No of visitors who read this post: 298
Category: MySQL Server
Type: Question
Author: Aaronsmith
No votes yet

I want to make a column that can add the records together on my MySQL database. The record will add up the score of my entire league, taking the points from a race and add it with another race. The purpose of this is to compute the overall points of my entire league for one season. How can I possibly perform it?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

# (Solution Accepted)

You need to apply the Aggregate function sum to perform the addition in your MySQL query.

Just type in

SELECT SUM(score) as total_score FROM race;

For example, you name the column that contains the score of the entire league as 'total score' and  'race' . The command added all the values in the score field and displayed the total score.

The SUM command will add all the values of the score field and will create a new column that will show the total score for the entire league.


#

To get the total score of the entire league table, you have to identify the table name (such as league_table) and the column where you want to get the total from (such as race). The code to get the total can be as shown below assuming you are using php to connect to server:

<?php

$query="Select Sum(race) from league_table";

$result=mysql_query($query) or die(mysql_error());

while($row=mysql_fetch_array($result));

{ echo "Total Score". "=" .$row['Sum(race)'];

   echo "<br>":

}

Thank you.