我希望更新我的chartofaccount以获得试算平衡,这是我在chartofaccount表中所拥有的
Id parent Debit Credit
1
10 1
101 10
1011 101 30
101121231 1011 40
101134121 1011 20
我需要表格看起来像这样
Id parent Debit Credit
1 60 30
10 1 60 30
101 10 60 30
1011 101 60 30
101121231 1011 40
101134121 1011 20
我的想法是我需要将账户ID与父母相加
这里有什么用,但只是更新1条记录
$select2="SELECT sum(debit),sum(credit),parent
FROM chartofaccount
group by parent
ORDER BY parent DESC";
$run2=mysql_query($select2,$con);
if(!$run2)die("ERROR22".mysql_error());
for($counter=0;$row1=mysql_fetch_array($run2);$counter++){
$parentid=$row1[2];
$update3="UPDATE chartofaccount
SET debit='$row1[0]',
credit='$row1[1]'
WHERE Id='$parentid'";
$run3=mysql_query($update3,$con);
if(!$run3)die("ERROR1".mysql_error());
}
答案 0 :(得分:0)
$select2="SELECT Distinct(parent) FROM chartofaccount
ORDER BY parent DESC";
$run2=mysql_query($select2,$con);
if(!$run2)die("ERROR22".mysql_error());
for($counter=0;$row1=mysql_fetch_assoc($run2);$counter++){
$parentid=$row1['parent'];
$select3="SELECT sum(debit),sum(credit),parent
FROM chartofaccount
WHERE parent='$parentid'
group by parent";
$run3=mysql_query($select3,$con);
if(!$run3)die("ERROR22".mysql_error());
for($counter=0;$row2=mysql_fetch_array($run3);$counter++){
$parentidz=$row2[2];
$update3="UPDATE chartofaccount
SET debit='$row2[0]',
credit='$row2[1]'
WHERE Id='$parentidz'";
$run4=mysql_query($update3,$con);
if(!$run4)die("ERROR1".mysql_error());
}
}