如何在mysql中汇总所有结果

时间:2017-01-15 10:14:36

标签: php mysql sql sum checksum

这是我的查询以获得所有权重。:

$sumQuery = $conn->query("SELECT weightage FROM  r_job_skill WHERE id_job = ".$jobID." AND gdskill = 1");

我得到的结果如下:

weightage
10
20
50

我想计算所有这些结果的总和我该怎么做...?

4 个答案:

答案 0 :(得分:3)

或简短:

SELECT SUM(weightage) 
FROM r_job_skill 
WHERE id_job = ".$jobID." 
AND gdskill = 1;

答案 1 :(得分:2)

您可以使用sum() mysql函数计算列的总和

您的代码看起来像这样

$sumQuery = $conn->query("SELECT sum(weightage) as total FROM  r_job_skill WHERE id_job = ".$jobID." AND gdskill = 1");
if ($sumQuery->num_rows > 0) {
while($row = $sumQuery->fetch_assoc()) {
        echo "Total: " . $row["total"];
    }
} else {
    echo "0 results";
}
$conn->close();

答案 2 :(得分:1)

尝试:

{{1}}

see example here

答案 3 :(得分:0)

试试这个:

SELECT SUM(weightage) FROM (SELECT weightage FROM  r_job_skill WHERE id_job = ".$jobID." AND gdskill = 1");