我有一个数据库的表tablea,结构和部分数据如下id   chargea  chargeb  chargec  charged
1    100          200   40       50
2    30           40    200      400
..我现在想列出这里所有的数据,并求每一条数据中 chargea chargeb chargec charged的和,以及最后一个总和列出来的结果是id   chargea  chargeb  chargec  charged     四个值的和
1    100          200   40       50          390
2    30           40    200      400         670
...
                                     总和:   1060<?php
            $exec="select * from tablea order by id desc"; 
                $result=mysql_query($exec,$conn);
                while($rs=mysql_fetch_object($result)){
            ?>
<?php echo $rs->id;?> <?php echo $rs->chargea;?> <?php echo $rs->chargeb;?> <?php echo $rs->chargec;?> <?php echo $rs->charged;?> <?php echo 这里的四个值的和怎么写呢?>
<?php } ?>      
这里的总和怎么写呢?请问上面四个值的和怎么写呢?总和怎么写呢?
                               

解决方案 »

  1.   

    select SUM(xxxx) AS xxx,SUM(xxxx) AS xxxx 
    就这么写SQL 语句 其他的自己分析下吧
      

  2.   


    <?php
    $exec="select *,chargea+chargeb+chargec+charged as sum_charge  from tablea order by id desc";  
    $sum=0
      $result=mysql_query($exec,$conn);
      while($rs=mysql_fetch_object($result)){
    ?>
    <?php echo "这里的四个值:".$rs->sum_charge?>
      
    <?
    $sum+=$rs["sum_charge"];  }
    ?>
    总和$sum