首页 > 解决方案 > 用 php/mysql 中的条件汇总多维数组中的值

问题描述

我在mysql中有下表

e_id p_id w1 w2 w3 w4
87 1019 1 1 0 0
87 1019 0 0 0 1
87 1020 1 1 1 1
87 1021 0 1 0 0
87 1021 0 0 1 1
87 1021 1 0 0 0
89 1020 1 1 1 1
89 1022 1 1 1 0
89 1022 0 0 0 1

我想对 e_id 和 p_id 相等的行求和。所以结果应该是这样的:

e_id p_id w1 w2 w3 w4
87 1019 1 1 0 1
87 1020 1 1 1 1
87 1021 1 1 1 1
89 1020 1 1 1 1
89 1022 1 1 1 1

我不确定我是否可以在 mysql 中做到这一点,可能只能在 php 中。获取后,我在 php 中得到了以下数组

$schedules[]= array('employee' => $e_id, 'project' => $p_id, 'weeks' => $weeks);


$e_id = fetched e_id from table
$p_id = fetched p_id from table
$weeks = fetched array of w1..w4 from table

我该怎么做?感谢任何帮助

标签: phpmysqlmultidimensional-arrayarray-sum

解决方案


select sum(w1) as w1,xxxxxxxxx from table_name group by e_id, p_id


推荐阅读