Php 如何将特定行的列中的记录和同一行中的答案相加

Php 如何将特定行的列中的记录和同一行中的答案相加,php,mysql,row,Php,Mysql,Row,我如何将费用、公交车和零食加起来,并将结果显示在“总计”下的同一行?e、 g 100+20+500=620 数据 salary recurrent repairs_maint ..... TOTAL ============================================= 100 20 250 250 ..... 620 ============================================== 20 10 20

我如何将费用、公交车和零食加起来,并将结果显示在“总计”下的同一行?e、 g 100+20+500=620

数据

salary recurrent repairs_maint .....   TOTAL
=============================================
100       20      250    250  .....     620
==============================================
20   10  200      100    100.....      430
==============================================
PHP代码

<?php 
      $add=mysqli_query($conn,'SELECT SUM(salary),SUM(recurrent),SUM(repairs_maint),SUM(creditors) ,SUM(petty_cash) ,SUM(capital_expenses)from `monthly_expenditure`');
      while($row1=mysqli_fetch_array($add))
      {
        $mark=$row1['SUM(salary)'];
        $mark1=$row1['SUM(recurrent)'];
        $mark3=$row1['SUM(repairs_maint)']; 
        $mark4=$row1['SUM(creditors)'];
        $mark5=$row1['SUM(petty_cash)'];
        $mark6=$row1['SUM(capital_expenses)'];
     ?>

      <tr>
      <th></th>
      <th>Total  =</th>
        <th><?php echo $mark ?></th>
        <th><?php echo $mark1 ?></th>
        <th><?php echo $mark3 ?></th>
        <th><?php echo $mark4 ?></th>
        <th><?php echo $mark5 ?></th>
        <th><?php echo $mark6 ?></th>

您可以从这里开始:

id expense value
 1 FEES      100  
 1 BUS        20   
 1 SNACKS    500    
 2 FEES       20
 2 BUS        10  
 2 SNACKS    400  

你的描述有点混乱。如果我理解正确,则您希望对每个id的所有给定属性求和。以下查询可能有效:

select t.id,t.a as salary,t.b as recurrent,t.c as repairs_maint,
       t.d as creditors,t.e as petty_cash,t.f as capital_expenses
       (t.a + t.b + t.c + t.d + t.e + t.f) as total
from
(SELECT SUM(salary) as a,
      SUM(recurrent) as b,
      SUM(repairs_maint) as c,
      SUM(creditors) as d,
      SUM(petty_cash) as e,
      SUM(capital_expenses) as f
from monthly_expenditure
group by id) t
group by t.id;

您应该使用别名-您的查询不包含这些列中的任何一列,如果这是应该表示的,除非您有未显示的HTML表格标题。您是否意外地包含了错误的代码?对不起,我不知道如何在此处发布问题这是我已接受的类似代码谢谢,我想要id=1的费用总额,请添加另一个字段名,比如说Bus,我想在每行末尾显示总额,而不是在正文的底部。您似乎对数据存储和数据显示之间的差异感到非常困惑。