Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.