PHP + MYSQL SUM在相同的字段值之间

时间:2017-01-15 10:28:22

标签: php mysql

我正在处理工单进度系统。

我有这个问题:

SELECT c.sheet_no
     , c.step
     , a.crew_est
     , a.manhour_est
     , c.act_hours
  FROM work_sheet_machine a 
  LEFT 
  JOIN machine_master_data b 
    ON a.machine_code = b.code
  JOIN daily_activity_control_card-man c 
    ON c.wo_number = a.wo_number
   AND c.sheet_no = a.sheet
   AND c.step = a.serial
  LEFT 
  JOIN machine_master_data d 
    ON c.machine_code = d.code
 WHERE a.wo_number = 'I7474549';

MYSQL将此结果返回给我:

+----------+------+----------+-------------+-----------+
| sheet_no | step | crew_est | manhour_est | act_hours |
+----------+------+----------+-------------+-----------+
|        1 |    1 |        1 |         0.5 |        35 |
|        1 |    2 |        2 |           2 |        11 |
|        1 |    2 |        2 |           2 |        11 |
|        1 |    3 |        2 |           3 |        11 |
+----------+------+----------+-------------+-----------+
4 rows in set (0.00 sec)

结果很好,但我需要为每个相同的步骤编号获得 total_act_hours ,因此我需要获得的结果如下所示(请点击运行代码段以查看结果):



<table border="1">
<tr>
<td> sheet_no </td>
<td> step </td>
<td> crew_est </td>
<td> manhour_est </td>
<td> act_hours </td>
<td> total_act_hours </td>
</tr>
<tr>
<td> 1 </td>
<td> 1 </td>
<td> 1 </td>
<td> 0.5 </td>
<td> 35 </td>
<td> 35 </td>
</tr>
<tr>
<td> 1 </td>
<td> 2 </td>
<td> 2 </td>
<td> 2 </td>
<td> 11 </td>
<td rowspan="2"> 11 </td>
</tr>
</tr>
<tr>
<td> 1 </td>
<td> 2 </td>
<td> 2 </td>
<td> 2 </td>
<td> 11 </td>
</tr>
<tr>
<td> 1 </td>
<td> 3 </td>
<td> 2 </td>
<td> 2 </td>
<td> 3 </td>
<td> 11 </td>
</tr>
</table>
&#13;
&#13;
&#13;

如何在MYSQL查询或MYSQL + PHP代码中实现此目的?

Anyhelp将不胜感激。 谢谢。

1 个答案:

答案 0 :(得分:1)

尝试添加子查询,并将总和添加到您的查询中。 Watch here