表UI视图:Mysql-Php
我的表格视图:表UI视图:Mysql-Php,php,mysql,Php,Mysql,我的表格视图: id | packetId | fweight | 1 | 002 | 150 | 2 | 002 | 230 | 3 | 002 | 200 | 但我希望我的表UI应该如下所示 : 我想避免重复(002)值,并且在最后一个插入id的另一列中应该显示fweight sum ID-主ID 我如何在M
id | packetId | fweight |
1 | 002 | 150 |
2 | 002 | 230 |
3 | 002 | 200 |
但我希望我的表UI应该如下所示
:
我想避免重复(002)值,并且在最后一个插入id的另一列中应该显示fweight sum
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++;
}
$result=mysqli_query($con,“从packetId='002'所在的产品中选择id、packetId、fweight);
$tmp=“”;
$ctr=0;
$total=0;
while($row=mysqli\u fetch\u数组($result))
{
回声“;
echo$row['id'];
回声“;
如果($ctr==0 | |$tmp!=$row['id']))
{
echo$row['packetId'];
}
回声“;
echo$row['fweight'];
$total=$total+$row['fweight'];
回声“;
如果($tmp!=$row['id']))
{
echo$总计;
$total=0;
$tmp=$row['id'];
}
回声“;
$ctr++;
}
在SQL中很难做到。你最好在你的程序逻辑中做到这一点。@juergen:你能指导我“程序逻辑”吗?你的前端(PHP)会做到这一点。@Christian:你能指导我如何在PHP中做到这一点吗,因为我仍然在学习很多关于PHP的知识,mysqlNo,我们不能为你做到这一切。尝试一下,如果你被卡住了,请准确地添加你在尝试中的位置以及问题所在。然后,我们可能会帮助您+1获得gr8支持,感谢您尝试此功能,您能在$tmp=“”)上指导我吗$ctr=0$总数=0。。。他们是干什么的?嗨,克里斯蒂安,你能帮我回答上面同样的另一个问题吗
$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++;
}