-1

我正在使用以下代码返回购买详细信息。显示结果如下

Name    Qty Value
Product1 30 1000
Product1 45 2000
Product2 20 1500
Product2 25 3000

怎么能总结出pdt的数量和价值并这样显示

Name    Qty Value
Product1 75 3000
Product2 45 4500
Total    120 7500

使用 Select Where 子句。

  $result = mysql_query("SELECT pdtname,pack,qty,value FROM ist WHERE  chemistcode='3121' AND chemistcode='3020' AND companycode = $companycode");

echo "<table id=customers>
<tr>
<th>Product</th>
<th>Pack</th>
<th>Qunatity</th>
<th>Value</th>
</tr>";


        while($row = mysqli_fetch_array($result))
  {
echo "<tr>";
echo"<td>" . $row['pdtname'] . "</td>";
echo"<td>" . $row['pack'] . "</td>";
echo"<td>" . $row['qty'] . "</td>";
echo"<td>" . $row['value'] . "</td>";
echo "</tr>";
  }
echo "</table>";
4

2 回答 2

1

sum()您可以使用聚合函数和使用以下方法轻松获得结果group by with rollup

select 
  coalesce(name, 'Total') Name, 
  sum(qty) qty,
  sum(value) value
from ist
group by name with rollup;

请参阅SQL Fiddle with Demo。这给出了结果:

|     NAME | QTY | VALUE |
| Product1 |  75 |  3000 |
| Product2 |  45 |  4500 |
|    Total | 120 |  7500 |
于 2013-09-11T18:41:29.637 回答
0
select name,sum(Qty),sum(value) from table2 
group by name
union
select 'total',sum(Qty),sum(value) from table2
于 2013-09-07T21:48:45.943 回答