【问题标题】:How to count the total of each duplicate values from MySQL in PHP?如何计算 PHP 中 MySQL 中每个重复值的总数?
【发布时间】:2017-02-23 14:47:20
【问题描述】:

如何计算来自 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");
    }

【问题讨论】:

标签: php mysql


【解决方案1】:

我可能推断您的问题表中有一个“itemid”列,但我不知道您发布的内容中有足够的信息可以提供帮助。

以下是查找重复项的方法 Finding duplicate values in MySQL

【讨论】:

    【解决方案2】:

    你应该试试这个:

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

    【讨论】:

      【解决方案3】:
      SELECT COUNT(itemid) FROM issues WHERE date='$date' GROUP BY itemid
      

      【讨论】:

        【解决方案4】:

        您可以将每个 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
        }
        

        【讨论】:

          【解决方案5】:

          终于找到了

          $sql = $conn->prepare("SELECT * ,itemid,count(*) AS total_records  FROM issues WHERE date='$date' GROUP BY itemid");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-09-11
            • 2020-04-05
            • 2020-05-13
            • 2011-10-03
            • 2014-11-07
            • 1970-01-01
            • 2012-05-11
            相关资源
            最近更新 更多