我有一个名为finance的MySQL表,它有id,balance,requested,ytotal6和budget列。 我想对余额列值求和,并将结果写入ytotal6,其中id为1列。
这是表结构link
这是我的 index.php 文件
$con=mysqli_connect("127.0.0.1","root","","dji001");
$result = mysqli_query($con,"SELECT * FROM finance")
or die("Error: ".mysqli_error($con));
echo "<form style='width:1080px; backgrond-color:transparent;' action='update.php' method='post' class='form-group'>";
while($row = mysqli_fetch_array($result))
{
$id = $row['ID'];
$Budget = $row['Budget'];
$Balance = $row['Balance'];
$Requested = $row['Requested'];
$balance = $Requested + $Budget;
$con->query("UPDATE finance SET balance = $balance WHERE id = $id");
echo "<div class='calc_container'>
<input type='hidden' class='id' name='id[]' value='".$row['ID']."'>
<input type='text' class='budget' name='Budget[]' value='".$row['Budget']."'>
<input type='text' class='req_kbl' name='Requested[]' value='".$row['Requested']."'>
<input type='text' class='balance' name='Balance[]' value='".$row['Balance']."'>
</div>";}
$result = mysqli_query($con,"SELECT * FROM finance where id=1")
or die("Error: ".mysqli_error($con));
while($row = mysqli_fetch_array($result))
echo"
<input type='text' class='ytotal6' name='ytotal6' value='".$row['ytotal6'] ."'>";
echo "<input type='Submit' class='button' value='Submit'></form>";
我尝试$ytotal6 = sum[$balance];
,但它没有用。
答案 0 :(得分:0)
对于&#34;求和平衡列值并将结果写入ytotal6,其中id为1列&#34;试试这个
UPDATE finance f1, (SELECT SUM(balance) AS bal
FROM finance ) f2
SET ytotal6 = bal
WHERE f1.id = 1;
注意:对于类似&#34的错误;您无法在FROM子句&#34;中指定要更新的目标表。我们需要创建一个临时表