PHP + MYSQL 相同字段值之间的求和

标签 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>

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

任何帮助将不胜感激。 谢谢。

最佳答案

尝试将子查询和 at sum 添加到查询中。 Watch here

关于PHP + MYSQL 相同字段值之间的求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41660104/

相关文章:

php - 如何存储有关表的元数据?

php - 如何使用 Composer 从本地存储库加载 php 类?

php - 3 列 html 适合 chrome,但它在 firefox 中搞砸了

php - 在 WordPress 插件中强制下载文件

mysql - 基于两个表之间字段比较的插入和更新

php - MySQL + PHP : ~500kb JSON file - Loading data as tables and fields VS as a single serialized variable

php - Vagrant/Puppet --- 确保 : change from present failed: Could not set 'present on ensure: No such file or dir

php - 如何从 PHP 脚本生成表格格式的输出

c# - 当我知道行的 id 时,如何使用单元格的值?

php - 如何在MySQL和php中检测记录是否被修改