user3147145
user3147145

Reputation: 59

Trying to multiply 2 column values together for each row & print the total sum of all the rows

My goal is to work out the total value of stock.

Table name: Stock
Column names: cost(representing an items cost), s_count(representing quantity).

cost multiplied by s_count would give the total value of each item. how would i go about multiplying these columns together, then totaling the results for all rows together to get the total amount?

$selectstock=$db->query("SELECT * FROM stock");
$result = $db->query('SELECT SUM(s_count) AS value_sum FROM stock'); 
$rowes = mysql_fetch_assoc($result); 
$sum = $rowes['value_sum'];
$result2 = $db->query('SELECT SUM(amountfailed) AS value_sum FROM stock'); 
$rowes2 = mysql_fetch_assoc($result2); 
$sum2 = $rowes2['value_sum'];
$uparts = mysql_num_rows($selectstock);
$value = // how do i do this?
print "<h3>Stock overview..</h3><br />
<div><p><br />
Amount of unique parts in stock: {$uparts}<br />
total quantity of stock: {$sum}<br />
Current value of all stock: {$value}<br />
Amount of parts failed: {$sum2}<br />";

if this was the table below... example:-

| id | cost | quantity |
| 1  |  20  |     5    |
| 2  |   5  |    10    |
| 3  |   2  |     2    |
| 4  |  10  |     1    |
| 5  |   7  |     3    |

then $value would need to be 20*5 + 2*10..etc

so $value would return 185.

Upvotes: 0

Views: 1844

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269573

Is this what you want?

SELECT SUM(s_count*cost) AS value_sum
FROM stock

Upvotes: 1

Related Questions