dreamwest Posted April 22, 2009 Share Posted April 22, 2009 I need to recalculate a whole column based on another columns row change Database structure: id | cost | total 1 | 20 | 20 2 | 40 | 60 3 | 10 | 70 4 | 70 | 140 5 | 60 | 200 etc... Say i use a form to edit a value from cost like this (in bold) id | cost | total 1 | 20 | 20 2 | 40 | 60 3 | 20 | 70 <-----New value from 10 to 20 in cost 4 | 70 | 140 5 | 60 | 200 etc... How can i recalculate the rest of the total column so it ends up like this: id | cost | total 1 | 20 | 20 2 | 40 | 60 3 | 20 | 80 4 | 70 | 150 5 | 60 | 210 Link to comment https://forums.phpfreaks.com/topic/155159-solved-help-with-query/ Share on other sites More sharing options...
Yesideez Posted April 22, 2009 Share Posted April 22, 2009 I don't feel there's any need to have that column to be honest. Let's say you pull the data resulting in row 4 getting pulled. We have ID of 4 and COST of 70. We can run a query to total up everything previously. SELECT SUM(cost) AS total FROM table WHERE id<=4 Link to comment https://forums.phpfreaks.com/topic/155159-solved-help-with-query/#findComment-816203 Share on other sites More sharing options...
dreamwest Posted April 22, 2009 Author Share Posted April 22, 2009 I don't feel there's any need to have that column to be honest. Let's say you pull the data resulting in row 4 getting pulled. We have ID of 4 and COST of 70. We can run a query to total up everything previously. SELECT SUM(cost) AS total FROM table WHERE id<=4 Your right, Thanks. I didnt think of doing it this way, this will get rid of half my code Link to comment https://forums.phpfreaks.com/topic/155159-solved-help-with-query/#findComment-816211 Share on other sites More sharing options...
FezEvils Posted April 22, 2009 Share Posted April 22, 2009 hello there... what happen to data id 4 and 5? another way is you just create sql update statement and add it with your form value Link to comment https://forums.phpfreaks.com/topic/155159-solved-help-with-query/#findComment-816214 Share on other sites More sharing options...
dreamwest Posted April 22, 2009 Author Share Posted April 22, 2009 Ok i rewrote the query to get the total of cost, but this is my first time using sum() and it doesnt seem to be working: $result = mysql_query("SELECT SUM(cost) AS total FROM table ") or die(mysql_error()); $row = mysql_fetch_array( $result ); $total = number_format($row['SUM(cost)']/100,2); echo $total; Link to comment https://forums.phpfreaks.com/topic/155159-solved-help-with-query/#findComment-816216 Share on other sites More sharing options...
FezEvils Posted April 22, 2009 Share Posted April 22, 2009 Ok i rewrote the query to get the total of cost, but this is my first time using sum() and it doesnt seem to be working: $result = mysql_query("SELECT SUM(cost) AS total FROM table ") or die(mysql_error()); $row = mysql_fetch_array( $result ); $total = number_format($row['SUM(cost)']/100,2); echo $total; edit this: $total = number_format($row['total']/100,2); //because you have set sum(cost) as total.. Link to comment https://forums.phpfreaks.com/topic/155159-solved-help-with-query/#findComment-816218 Share on other sites More sharing options...
Yesideez Posted April 22, 2009 Share Posted April 22, 2009 I wouldn't just "copy & paste" the query I posted into your code and expect it to work straight away. I didn't know what your tables were called so I used "table" - this needs to be replaced with the name of your table. $total = number_format(($row['total']/100),2); Have added brackets to the value to make sure that gets done first (the division) before it gets passed to number_format() Link to comment https://forums.phpfreaks.com/topic/155159-solved-help-with-query/#findComment-816221 Share on other sites More sharing options...
FezEvils Posted April 22, 2009 Share Posted April 22, 2009 ya, Yesideez right.. it's called BODMAS www.mathsisfun.com/operation-order-bodmas.html Link to comment https://forums.phpfreaks.com/topic/155159-solved-help-with-query/#findComment-816225 Share on other sites More sharing options...
Yesideez Posted April 22, 2009 Share Posted April 22, 2009 ya, Yesideez right.. it's called BODMAS www.mathsisfun.com/operation-order-bodmas.html You got any idea how long it's been since I have seen or heard anyone say that word? It's been many, many YEARS! Link to comment https://forums.phpfreaks.com/topic/155159-solved-help-with-query/#findComment-816227 Share on other sites More sharing options...
FezEvils Posted April 22, 2009 Share Posted April 22, 2009 hehe..me too... ;D Link to comment https://forums.phpfreaks.com/topic/155159-solved-help-with-query/#findComment-816229 Share on other sites More sharing options...
dreamwest Posted April 22, 2009 Author Share Posted April 22, 2009 I wouldn't just "copy & paste" the query I posted into your code and expect it to work straight away. I didn't know what your tables were called so I used "table" - this needs to be replaced with the name of your table. $total = number_format(($row['total']/100),2); Have added brackets to the value to make sure that gets done first (the division) before it gets passed to number_format() Woops - i didnt notice the AS total. I figured it out Thanks Link to comment https://forums.phpfreaks.com/topic/155159-solved-help-with-query/#findComment-816266 Share on other sites More sharing options...
Yesideez Posted April 22, 2009 Share Posted April 22, 2009 btw, AS is handy to rename a column or data returned. Particularly handy when you're performing a mathematical operation and want to give the value an index name to reference it by, just like we have with our SUM value - calling it "total" Link to comment https://forums.phpfreaks.com/topic/155159-solved-help-with-query/#findComment-816300 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.