使用 php/mysql 中的条件汇总多维数组中的值
Summarise values in multidimensional array with condition in php/mysql
我在 mysql
中有以下 table
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
我该怎么做?感谢任何帮助
select sum(w1) as w1,xxxxxxxxx from table_name group by e_id, p_id
您应该在两列上应用 group by
。应该是这样的:
SELECT
e_id,
p_id,
SUM(w1) as w1,
SUM(w2) as w2,
SUM(w3) as w3,
SUM(w4) as w4
FROM schedule
GROUP BY e_id, p_id
我在 mysql
中有以下 tablee_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
我该怎么做?感谢任何帮助
select sum(w1) as w1,xxxxxxxxx from table_name group by e_id, p_id
您应该在两列上应用 group by
。应该是这样的:
SELECT
e_id,
p_id,
SUM(w1) as w1,
SUM(w2) as w2,
SUM(w3) as w3,
SUM(w4) as w4
FROM schedule
GROUP BY e_id, p_id