我想要排和&显示与剩余列相同的行中的结果,但我得到的结果(第一行的总和)在这里是我的PHP代码
<?php
$sub = mysql_connect ("localhost", "root", "");
if (!$sub)
{
die('Could Not Connect:' .mysql_error());
}
mysql_select_db("tabsc",$sub);
if (!empty($_POST))
{
$sql= "INSERT INTO fund (dat, sour, condi, amount, disburse, othe, expenditure)
VALUES ('$_POST[dat]','$_POST[sour]','$_POST[condi]','$_POST[amount]','$_POST[disburse]','$_POST[othe]','$_POST[expenditure]')";
header('location: funding.php');
if (!mysql_query($sql, $sub))
{
die('Error' .mysql_error());}}
//start add sql column value on the top
$result = mysql_query('SELECT SUM(amount) AS amount FROM fund');
$row = mysql_fetch_assoc($result);
$sum = $row['amount'];
$result = mysql_query('SELECT SUM(disburse) AS disburse FROM fund');
$row = mysql_fetch_assoc($result);
$sumd = $row['disburse'];
$result = mysql_query('SELECT SUM(othe) AS othe FROM fund');
$row = mysql_fetch_assoc($result);
$sumo = $row['othe'];
$result = mysql_query('SELECT SUM(expenditure) AS expenditure FROM fund');
$row = mysql_fetch_assoc($result);
$sume = $row['expenditure'];
$remh=$sum-($sumd+$sumo+$sume);
//end add sql column value on the top
$resul=mysql_query("SELECT * FROM fund");
echo "<table border='1'>
<tr>
<th></th>
<th></th>
<th>Total</th>
<th>$sum</th>
<th>$sumd</th>
<th>$sumo</th>
<th>$sume</th>
<th>$remh</th>
<th></th>
<tr>
<th>Date</th>
<th>Source</th>
<th>Condition</th>
<th>Amount</th>
<th>Disburse</th>
<th>Other Payament</th>
<th>Expenditure</th>
<th>Remaining</th>
<th>Delete/Edit</th>
</tr>";
//Row Wise Sum (I think problem is here)
$result =mysql_query('SELECT id, SUM( amount + disburse + othe + expenditure) AS remaining FROM fund GROUP BY id');
$row = mysql_fetch_assoc($result);
$sumrem = $row['remaining'];
while ($row = mysql_fetch_array($resul))
{
echo '<tr>';
echo '<td>' .$row['dat'].'</td>';
echo '<td>' .$row['sour'].'</td>';
echo '<td>' .$row['condi'].'</td>';
echo '<td>' .$row['amount'].'</td>';
echo '<td>' .$row['disburse'].'</td>';
echo '<td>' .$row['othe'].'</td>';
echo '<td>' .$row['expenditure'].'</td>';
echo "<td>$sumrem</td>"; //Result will be into this columne
echo '<td><a href="delete.php? id='.$row['id'].'">Del</a> || <a href="edit.php? id='.$row['id'].'">Edit</a>';
echo '</tr>';
}
echo '</table>';
mysql_close($sub);
?>
答案 0 :(得分:0)
答案 1 :(得分:0)
如果您想获得所有行的总和,而不仅仅是第一行,请不要使用GROUP BY
。
$result =mysql_query('SELECT SUM( amount + disburse + othe + expenditure) AS remaining FROM fund');
$row = mysql_fetch_assoc($result);
$sumrem = $row['remaining'];
您还可以将所有其他SELECT SUM()
个查询合并到一个查询中:
$result = mysql_query('SELECT SUM(amount) AS amount, SUM(disburse) AS disburse, SUM(othe) as othe, SUM(expenditure) AS expenditure FROM fund');
$row = mysql_fetch_assoc($result);
$sum = $row['amount'];
$sumd = $row['disburse'];
$sumo = $row['othe'];
$sume = $row['expenditure'];
$remh = $sum - $sumd - $sumo - $sume;
$sumrem = $sum + $sumd + $sumo + $sume;
答案 2 :(得分:0)
谢谢每一位...... 我有一个想法&amp;它的工作正常。 将数据输入DB后,我在表
中运行此查询PreferenceScreen
然后将结果回显到剩余的列中。