0

我在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

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

4

2 回答 2

1

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

于 2021-08-16T09:57:46.433 回答
1

You should apply group by on two columns. It should be like this:

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
于 2021-08-16T09:58:12.677 回答