Jump to content

Sum over columns for each row


Zojak_Quaguz

Recommended Posts

Hello,

 

I have a table like this:

 

Name,Jan,Feb,Mar

John,1,5,2

Steve,2,9,3

Eric,4,4,1

 

I want to add a column to the table so it looks like this:

 

Name,Jan,Feb,Mar,Total

John,1,5,2,8

Steve,2,9,3,14

Eric,4,4,1,9

 

So that for each row, the 'Total' column is a sum over multiple columns (in this case, Jan, Feb, and Mar).

 

I think I can generate the 'Total' column by

 

SELECT Jan+Feb+Mar

 

But how do I add it to the table?

Link to comment
https://forums.phpfreaks.com/topic/225742-sum-over-columns-for-each-row/
Share on other sites

If you're saying that you actually want to add a new field to the database table, you don't want to do that. A database is not a spreadsheet. If that isn't what you mean, can you clarify your question?

What Pikachu2000 is saying is correct.  You can select the total but probably don't want the total in the db:

 

SELECT Name, Jan+Feb+Mar as Total FROM table_name

 

If you must add a column (not recommended), then use:

 

UPDATE table_name SET Total=Jan+Feb+Mar

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.