我的目标是计算出股票的总价值。
Table name: Stock
Column names: cost(representing an items cost), s_count(representing quantity).
成本乘以s_count将得出每个项目的总价值。 我将如何将这些列相乘,然后将所有行的结果合计在一起以获得总量?
$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 />";
如果这是下表...... 例如: -
| id | cost | quantity |
| 1 | 20 | 5 |
| 2 | 5 | 10 |
| 3 | 2 | 2 |
| 4 | 10 | 1 |
| 5 | 7 | 3 |
然后$value
需要20 * 5 + 2 * 10..etc
所以$ value将返回185。
答案 0 :(得分:1)
这是你想要的吗?
SELECT SUM(s_count*cost) AS value_sum
FROM stock