【问题标题】:Summarise values in multidimensional array with condition in php/mysql用 php/mysql 中的条件汇总多维数组中的值
【发布时间】:2021-10-18 09:09:52
【问题描述】:

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

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

【问题讨论】:

    标签: php mysql multidimensional-array array-sum


    【解决方案1】:

    您应该在两列上应用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
    

    【讨论】:

      【解决方案2】:

      从表名组中选择 sum(w1) as w1,xxxxxxxxx by e_id, p_id

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-06
        • 1970-01-01
        • 2018-02-28
        • 1970-01-01
        相关资源
        最近更新 更多