我的表视图:
id | packetId | fweight |
1 | 002 | 150 |
2 | 002 | 230 |
3 | 002 | 200 |
但我希望我的表UI应该如下所示:
id | packetId | fweight | sum(fweight)
1 | 002 | 150 |
2 | | 230 |
3 | | 200 | 580
我想避免重复(002)值,而且fweight sum应该显示在最后一个插入id的另一个列中。
ID-主要ID,
我如何在Mysql,php中获得它。
查询我目前所知:
"SELECT id, packetId , fweight from product where packetId="002" ";
假设您拥有所有的头文件/conn等
试试这个:
$result = mysqli_query($con,"SELECT id, packetId , fweight from product where packetId='002'");
$tmp = "";
$ctr = 0;
$total = 0;
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
if($ctr == 0 || $tmp != $row['id'])
{
echo $row['packetId'];
}
echo "</td><td>";
echo $row['fweight'];
$total = $total + $row['fweight'];
echo "</td><td>";
if($tmp != $row['id'])
{
echo $total;
$total = 0;
$tmp = $row['id'];
}
echo "</td></tr>";
$ctr++;
}