MYSQL查询的总和

时间:2018-12-29 14:41:59

标签: php mysql sum

我有一个MySQL数据库

我想获取列中值的总和或总和,我需要将其回显以用于Google Graphs

这是我当前的代码

 <?php 
        $query = "SELECT * from activation";

         $exec = mysqli_query($con,$query);
         while($row = mysqli_fetch_array($exec)){

         echo "['".$row['type']."',".$sum['type']."],"; 
         }
         ?> 

我想获取SUM值的行类型不同于HIJACKING ACCIDENT,所以值是恒定的。

我知道的$ sum错误,但这是我需要回显行类型总数的地方

以下代码有效

 <?php 
            $query = "SELECT type, count(type) from activation group by type";

             $exec = mysqli_query($con,$query);
             while($row = mysqli_fetch_array($exec)){

             echo "['".$row['type']."',".$row['count(type)']."],";
             }
             ?> 

1 个答案:

答案 0 :(得分:1)

我想这是您应该使用的更精确的代码。我将您的count(type)的别名用作act_type。希望这会有所帮助:)

<?php 
    $query = "SELECT type, count(type) as act_type from activation group by type";
    $exec = mysqli_query($con,$query);
    $expected = [];
    while($row = mysqli_fetch_array($exec)){
         $expected[$row['type']] = $row['act_type'];
         echo "['".$row['type']."',".$row['act_type']."],"; // you can use this line just for printing purpose
   }
   // use $expected array as you wish any where in your code, it contains your result as key=>value manner
?>