nitation Posted July 14, 2008 Share Posted July 14, 2008 Hello guys, I believe it is possible to SUM all the columns in a MYSQL table. How do i UNSUM or Subtract all the columns in a mysql table also. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/114740-solved-sum-and-subtract-in-mysql/ Share on other sites More sharing options...
kenrbnsn Posted July 14, 2008 Share Posted July 14, 2008 What do you mean? What are you subtracting the values from? Ken Quote Link to comment https://forums.phpfreaks.com/topic/114740-solved-sum-and-subtract-in-mysql/#findComment-590004 Share on other sites More sharing options...
nitation Posted July 14, 2008 Author Share Posted July 14, 2008 @ken If i have three columns like this; column1 =100 column2 =200 column2 =300 to sum them, i believe the query is like this select SUM(column) AS Total FROM table_name where xx='xx' This will add the values together and give me 600. Good. My question now is, i am inserting a new value which is 1000, how do i minus it from the last three values. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/114740-solved-sum-and-subtract-in-mysql/#findComment-590018 Share on other sites More sharing options...
.josh Posted July 14, 2008 Share Posted July 14, 2008 well you can't do select subqueries inside a modify query so you're gonna have to break it down into 2 separate queries. The first query would be the one you already have. The second query will be a plain run of the mill insert query where you just do 1000-$Total as the value ($Total being the retrieved info from the first query). Quote Link to comment https://forums.phpfreaks.com/topic/114740-solved-sum-and-subtract-in-mysql/#findComment-590037 Share on other sites More sharing options...
nitation Posted July 15, 2008 Author Share Posted July 15, 2008 Let me break what am trying to do . A user's account was credited with the below number by the administrator in the admin area; row1 =100 row2 =200 row3 =5000 This was added together using (SUM) in the mysql query to get the Total balance, which is 5800. The user then try to transfer the amount credited. This works fine. The problem is, if i update the table where the amount was credited, to derive the user's balance, this update all the amount in the rows. If i insert the amount the user transferred into the same table on a new row, on the balance page, it will SUM all the values in the row. Now instead of the balance decreasing, it is forever increasing. How do i derive the amount transferred by the user and echo it on the balance page. Do i keep it on a separate table or what. Please help This is running me off. Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/114740-solved-sum-and-subtract-in-mysql/#findComment-590047 Share on other sites More sharing options...
smstromb Posted July 15, 2008 Share Posted July 15, 2008 Crayon Violent is right. Just make 2 separate queries. Store the sum in a variable like $total and then subtract the amount you want with simple php arithmetic. Store the answer in another variable, and then insert that final variable into the table of your choice using a second query. Hope that helped. Quote Link to comment https://forums.phpfreaks.com/topic/114740-solved-sum-and-subtract-in-mysql/#findComment-590053 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.