如何计算PHP中MySQL的每个重复值的总和?

时间:2017-02-23 14:47:20

标签: php mysql

如何计算来自MySQL的重复“itemid”条目。下面的代码导出MySQL中的结果,但我想计算每个重复“itemid”的总数。

实施例: 输出(122,133,122,122,133,188)。 122 = 3,133 = 2,188 = 1.

if(isset($_POST['daily']) && isset($_POST['reportdate'])){
     global $conn;
     $date = $_POST['reportdate'];
        $sql = $conn->prepare("SELECT * FROM issues WHERE date='$date'");
        $sql->execute();

        $output .='
        <table class="table" bordered="1">
            <tr>
                <th class="green">SAPCODE</th>
                <th class="green">DATE</th>
                <th class="green">DESCRIPTION</th>
                <th class="green">QUANTITY</th>
                <th class="green">UNIT</th>
                <th class="green">ISSUED TO</th>

            </tr>
        ';

        while($row = $sql->fetch(PDO::FETCH_ASSOC)){
                $perstat->getID($row['empid']);
                 $stock->getItemByID($row['itemid']);
                  $time = strtotime($row['date']);
                     $row['date']= date("d-M-y", $time);

                 $output .='

                     <tr>
                        <td>'.$row['itemid'].'</td>
                        <td>'.$row["date"].'</td>
                        <td>'.$stock->description.'</td>
                        <td>'.$row["qty"].'</td>
                        <td>'.$stock->unit.'</td>
                        <td>'.$perstat->pats.'</td>               
                     </tr>

                 ';
        }
        $output .='</table>';
        header("Content-Type: application/xls");
        header("Content-Disposition:attachment; filename=PPE Issuance report .xls");
        header("Pragma: no-cache"); 
        header("Expires: 0");
        echo $output;
    }else{
        header("Location:issuelist.php");
    }

4 个答案:

答案 0 :(得分:0)

我可能会推断出有一个列&#34; itemid&#34;在您的问题表中,但我不知道您发布的内容中有足够的信息可以提供帮助。

以下是您将如何找到重复项 Finding duplicate values in MySQL

答案 1 :(得分:0)

你应该试试这个:

SELECT CONCAT(itemid,count(itemid)) FROM issues WHERE date='$date'" GROUP BY itemid

答案 2 :(得分:0)

SELECT COUNT(itemid) FROM issues WHERE date='$date' GROUP BY itemid

答案 3 :(得分:0)

您可以将每个itemID作为索引添加到数组中,并在查询结果时记录它们。

例如(代码注释了更多解释):

$item_id_count = array(); // Declare an empty array to keep count of itemIDs

while($row = $sql->fetch(PDO::FETCH_ASSOC)){
  $perstat->getID($row['empid']);
  $stock->getItemByID($row['itemid']);
  $time = strtotime($row['date']);
  $row['date']= date("d-M-y", $time);

  $output .='<tr>
                <td>'.$row['itemid'].'</td>
                <td>'.$row["date"].'</td>
                <td>'.$stock->description.'</td>
                <td>'.$row["qty"].'</td>
                <td>'.$stock->unit.'</td>
                <td>'.$perstat->pats.'</td>               
             </tr>';

  // Add itemID index into array with value of 0 if it does not exist
  if(!isset($item_id_count[$row['itemid']])){
    $item_id_count[$row['itemid']] = 0;
  }
  // Increment the value of the itemID index in array when it does exist
  $item_id_count[$row['itemid']]++;
}

// Print out each itemID with its count from the array.
foreach($item_id_count as $itemID => $itemIDCount){
  echo $itemID ." - " . $itemIDCount
}