2017-02-23 1 views
1

Wie zähle ich doppelte "itemid" -Einträge von MySQL? Der Code unterhalb der Exporte führt zu MySQL, aber ich möchte die Summe jedes Duplikats "itemid" zählen. Beispiel: Ausgabe (122,133,122,122,133,188). 122 = 3, 133 = 2, 188 = 1.Wie zählt man die Summe aller doppelten Werte von MySQL in PHP?

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"); 
    } 
+0

Hinweis: 'GROUP BY'. –

+0

Mögliches Duplikat von [Doppelte Werte in MySQL finden] (http://stackoverflow.com/questions/688549/finding-duplicate-values-in-mysql) – davejal

+0

Definitiv ein Duplikat von http://stackoverflow.com/q/688549/3664960, so viele Antworten dort mit vielen upvotes – davejal

Antwort

0

entnehme ich, dass es eine Spalte „itemid“ in Ihren Fragen Tisch ist, aber ich weiß nicht, dass es genug Informationen, was Sie von Hilfe sein geschrieben.

Hier ist, wie Sie Duplikate Finding duplicate values in MySQL

0

finden würden Sie sollten dies versuchen:

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

Sie jeweils als Index und halten Zählung von ihnen, wie Sie Itemid zu einem Array hinzufügen könnte Durchlaufen Sie Ihre Ergebnisse aus Ihrer Anfrage.

Zum Beispiel (Code kommentiert für weitere Erklärung):

$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 
} 
0

Schließlich fand ich es

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