Summation of Columns in SQL

cooldev007

Broken In
Joined
Oct 7, 2005
Messages
89
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

I am the master of my Fate.
Joined
Jan 8, 2008
Messages
12,332
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....:-o
 

furious_gamer

Excessive happiness
Joined
Jun 17, 2008
Messages
4,246
@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

God of Mistakes...
Joined
Dec 26, 2005
Messages
1,896
IMO, View is the ideal for this. Because it won't store total actually, but will calculate it when needed.
 

Zangetsu

I am the master of my Fate.
Joined
Jan 8, 2008
Messages
12,332
i got the solution:
just use default constraint on the table.....
:grin: no need of SP & trigger @ all
 
OP
C

cooldev007

Broken In
Joined
Oct 7, 2005
Messages
89
Thank To you all for your posts... How Can I Do this in Access 2007? Kindly Reply
 
Top