PHP的SQL数据库的行明智的总和

时间:2016-12-29 20:02:57

标签: php sql

我想要排和&显示与剩余列相同的行中的结果,但我得到的结果(第一行的总和)在这里是我的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);
   ?>

3 个答案:

答案 0 :(得分:0)

  • 我认为您需要在循环内运行该查询以将ID传递给 获得剩余的每行,否则你不知道具体是什么 行,不要依赖id命令。
  • 尽量减少对数据库的调用次数,我个人会选择所有,然后使用php表达式求和并获得总计,并获得该行调用的剩余行数。
  • 如果您在单独的交易中进行如此多的调用,例如获得总计,您可能会遇到可能让用户在获得总数时更改金额的风险,从而显示错误的金额。因此,获取所有列加上剩余的计算列,然后在php
  • 中计算总计

答案 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

然后将结果回显到剩余的列中。