id name amt %
1 A 3000 100%
2 B 1500 45%
现在我有SUM(amount)*A(amount)/100
的总和,我得到了100%作为A&#39的贡献
现在,如果我们有另一个伙伴输入第二个值,它将计算新总值的百分比,但我希望旧值(即100%)也应该重新计算,因为总数现在增加了。就像我明智的那样,我有很多行。它应该以新的总数来计算。
我被困在这里,使用php。
代码:
$result = mysql_query("SELECT SUM(amount) AS val_sum FROM member_detail") or die(mysql_error());
$row = mysql_fetch_assoc($result);
$sum = $row['val_sum'];
$sumall = $sum+$amount;
//echo ($sumall);
$avg = $amount/$sumall*100;
$sql = "insert into investment (mem_name,p_amount,percentage) values ('$name','$amount','$avg')";
$query = mysql_query($sql) or die(mysql_error());
$obj->insert("member_detail",$_POST);
$msg = "Member added Successfully";
答案 0 :(得分:0)
试试这段代码
<?php
$obj->insert("member_detail",$_POST);
$result = mysql_query("SELECT SUM(amount) AS val_sum FROM member_detail")or die(mysql_error());
$row = mysql_fetch_assoc($result);
$sum = $row['val_sum'];
$sql = "insert into investment (mem_name,p_amount,percentage) values ('$name','$amount',0)";
$query = mysql_query($sql) or die(mysql_error());
$result = mysql_query("SELECT id AS val_sum FROM member_detail")or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$sumall = $row['val_sum'];
$avg = $amount/$sumall*100;
$sql = "update investment set percentage='$avg' where id=".$row['id'];
$query = mysql_query($sql) or die(mysql_error());
}
$msg = "Member added Successfully";