# Summation of Columns in SQL



## cooldev007 (Jul 1, 2010)

Suppose I have 3 columns of 3 different subjects say History, Maths and Science. Now I want to add the total of their values to another column say Total. Is it possible in MySQL? Is there any function for doing this. Also MySQL should automatically update the values when its data is stored in 3 columns of 3 different subjects. For e.g., Somebody has entered History Marks and another has entered Maths Marks. Then MySQL should automatically calculate the sum of History and Maths and keep the total even though the value of Science has not been entered. How this can be done with PHP and MySQL? Please Help


----------



## Zangetsu (Jul 1, 2010)

cooldev007 said:


> Suppose I have 3 columns of 3 different subjects say History, Maths and Science. Now I want to add the total of their values to another column say Total. Is it possible in MySQL? Is there any function for doing this. Also MySQL should automatically update the values when its data is stored in 3 columns of 3 different subjects. For e.g., Somebody has entered History Marks and another has entered Maths Marks. Then MySQL should automatically calculate the sum of History and Maths and keep the total even though the value of Science has not been entered. How this can be done with PHP and MySQL? Please Help



i dont abt MySQL but in SQL u can create a trigger for the requirement u have....


----------



## furious_gamer (Jul 1, 2010)

@OP

Nope, MySQL doesn't do this. You have to write a trigger or a Stored Procedure to achieve this.

Just learn trigger and SP and this task is very easy as it sounds, trust me.


----------



## Garbage (Jul 1, 2010)

IMO, View is the ideal for this. Because it won't store total actually, but will calculate it when needed.


----------



## furious_gamer (Jul 1, 2010)

^^



Can you elaborate this? I dont get you.


----------



## Zangetsu (Jul 1, 2010)

i got the solution:
just use default constraint on the table.....
 no need of SP & trigger @ all


----------



## cooldev007 (Jul 7, 2010)

Thank To you all for your posts... How Can I Do this in Access 2007? Kindly Reply


----------

