Guest bennyuk Posted October 30, 2005 Share Posted October 30, 2005 (edited) Hi If I have a table (tableA) with a rows with 2 decimal fields (field1 & field2), and I want to find the total value of field1 * field2 for each row and add them all together, is there 1 line in mysql that would do it. All I can think of at the moment is a WHILE loop with a running total, but I'm sure there mist be one nice mysql statement tha tcan return the result?:- $sql_select = mysql_query("SELECT * FROM tableA"); $count1 = 0; while ($row = mysql_fetch_array($sql_select)) { $fieldA = $row["fieldA"]; $fieldB= $row["fieldB"]; $count1 = $count1 + ($fieldA * $fieldB); } echo "result = $count1"; PS I'm stuck on old MySQL Version 3.23.54 Any ideas? PS excuse my typos in the title ( ) Edited October 30, 2005 by bennyuk Quote Link to comment Share on other sites More sharing options...
Guest estelle Posted October 30, 2005 Share Posted October 30, 2005 Try this: SELECT SUM(field1*field2) AS sum FROM tableA FYI - mysql.com is incredibly handy. Quote Link to comment Share on other sites More sharing options...
Guest bennyuk Posted October 31, 2005 Share Posted October 31, 2005 BRILLIANT, Bang on!, many thanx, 1 gold star owed to you. :D Quote Link to comment Share on other sites More sharing options...
Guest estelle Posted October 31, 2005 Share Posted October 31, 2005 A gold star........ yippee!! :D Quote Link to comment 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.