Zojak_Quaguz Posted January 26, 2011 Share Posted January 26, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/225742-sum-over-columns-for-each-row/ Share on other sites More sharing options...
Pikachu2000 Posted January 26, 2011 Share Posted January 26, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/225742-sum-over-columns-for-each-row/#findComment-1165504 Share on other sites More sharing options...
Zojak_Quaguz Posted January 26, 2011 Author Share Posted January 26, 2011 Well, the only number I am interested in is the total. The data just happens to come with subtotals for each month. So yes, I want to add a new field which is the sum of all the months. Quote Link to comment https://forums.phpfreaks.com/topic/225742-sum-over-columns-for-each-row/#findComment-1165505 Share on other sites More sharing options...
AbraCadaver Posted January 26, 2011 Share Posted January 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/225742-sum-over-columns-for-each-row/#findComment-1165552 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.