如何从不同条件的同一表中汇总数据?

时间:2017-08-16 05:31:47

标签: php sum

我从同一个表名中获取了一些数据,但条件不同,我查询了3次,结果是:

101=>1
301=>1
501=>2
502=>4
---------------
101=>2
501=>1
---------------
101=>1
501=>1

其中第一列是教室,第二列是值。在同一个教室中对这些值进行求和的最佳方法是什么,结果如下:

101 = 4
301 = 1
501 = 4
502 = 4

我的查询命令:

$query = $db->prepare("SELECT COUNT(std_id) AS total, std_class  FROM attendance WHERE att_mode IN('mode-01','mode-04') AND att_attend='0' AND att_date=CURDATE()  GROUP BY std_class ORDER BY std_class");
    $query->execute();
    while ($char = $query->fetch(PDO::FETCH_OBJ)) {
        $labels2[] = $char->std_class.'-'.$char->total;
        $link[] = $char->std_class;
    }
$query = $db->prepare("SELECT COUNT(std_id) AS total_sick,std_class FROM attendance WHERE att_mode ='mode-01' AND att_attend='2' AND att_date=CURDATE()  GROUP BY std_class ORDER BY std_class");
    $query->execute();
    while($char = $query->fetch(PDO::FETCH_OBJ)){
        $sickCount[] = $char->std_class.'-'.$char->total_sick;
    }
$query = $db->prepare("SELECT COUNT(std_id) AS total_leave,std_class FROM attendance WHERE att_mode ='mode-01' AND att_attend='3' AND att_date=CURDATE()  GROUP BY std_class ORDER BY std_class");
    $query->execute();
    while($char = $query->fetch(PDO::FETCH_OBJ)){
        $leaveCount[] = $char->std_class.'-'.$char->total_leave;
    }

4 个答案:

答案 0 :(得分:0)

使用子查询尝试这个。

"SELECT std_class , COUNT(std_id) as total ,(SELECT COUNT(std_id) AS total_sick FROM attendance WHERE att_mode ='mode-01' AND att_attend='2' AND att_date=CURDATE()  GROUP BY std_class ORDER BY std_class ) AS total_sick    , (SELECT COUNT(std_id) FROM attendance WHERE att_mode ='mode-01' AND att_attend='3' AND att_date=CURDATE()  GROUP BY std_class ORDER BY std_class) AS total_leave FROM attendance WHERE att_mode IN('mode-01','mode-04') AND att_attend='0' AND att_date=CURDATE()  GROUP BY std_class ORDER BY std_class "

如果不工作,请告诉我......

答案 1 :(得分:0)

编辑:Priyank的答案显然更好,因为它是通过SQL完成的,所以没有PHP开销。

我将获取每个查询的结果,将其粘贴到关联数组中,然后循环它们。它看起来像这样:

$result = db_query1();
$total = array();
foreach ($result as $classroom => $value) {
    $total[$classroom] += $value;
}

$result = db_query2();
foreach ($result as $classroom => $value) {
    $total[$classroom] += $value;
}

$result = db_query3();
foreach ($result as $classroom => $value) {
    $total[$classroom] += $value;
}

print_r($total);

答案 2 :(得分:0)

如上所述,Priyank的答案应该是要走的路,但是在看到其他答案之前我为你写了一个小函数我会发布它,因为如果你不想要它可能会有用任何理由都在SQL中完成。

function mergeAndSum() {
    // get all the given arguments and put them into an array
    $arrays = func_get_args();

    // create an array that will contain the output
    $output = array();

    // loop through all the arguments
    foreach($arrays as $array) {
        // if an argument is not an array it will be skipped
        if(!is_array($array)) {
            continue;
        }
        // otherwise it will be iterated
        foreach($array as $key => $value) {
            // if the key is already in the $output array
            if(array_key_exists($key,$output)) {
                // add the value to the existing onve for that key
                $output[$key] += $value;
            } else {
                // else store in the $output array the key with it's value
                $output[$key] = $value;
            }

        }

    }

    return $output;

}

所以,假设你有三个包含查询结果的数组

$link = array(
    101=>1,
    301=>1,
    501=>2,
    502=>4,
);
$sickCount = array(
    101=>2,
    501=>1
);
$leaveCount = array(
    101=>1,
    501=>1
);

你只需将它们传递给

$output = mergeAndSum($link,$sickCount,$leaveCount);

并且您的$输出将按照需要

(
    101 => 4
    301 => 1
    501 => 4
    502 => 4
)

请记住SQL方法会更好,这个函数足够灵活,可以用于其他类似的情况,你需要有一些数组来合并和求和

答案 3 :(得分:0)

我终于找到了解决方案。我只运行一个查询,而不是进行3次查询。 工作查询是:

$query = $db->prepare("SELECT COUNT(std_id) AS total, std_class FROM attendance WHERE att_mode IN('mode-01','mode-04') AND att_attend IN('0','2','3') AND att_date=CURDATE() GROUP BY std_class ORDER BY std_class");

“WHERE”子句将满足包含“0”,“2”和“3”值的“att_mode”。然后我得到了理想的结果。 感谢以前的所有答案。