資料都已經撈出來至陣列中了
請問有什麼方法可以比較當 'work_order_no','created_at','DDTEXT',{'position'}這四個欄位值相同時只顯示一筆資料就好,並計算有多少筆資料是相同的?
程式碼 :
$sql = "SELECT a.work_order_no,a.product_no,b.ARBPL2,substring(c.created_at,1,10) as created_at ,c.ng_no,d.DDTEXT,c.data,c.ng_count,c.machine_work_id
FROM machine_work a,machine b,machine_work_detail c,ngreason d
WHERE a.id = c.machine_work_id and a.machine_no = b.ARBPL and c.ng_no = d.ZZDEFECT and c.ng_count > 0 and c.data !=''
" ;
$sql.="ORDER BY `a`.`work_order_no` ASC,`c`.`ng_no` ASC";
//echo $sql;
$result = $pdo->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
$obj = json_decode($row['data'],false);
print_r($row);
echo "<br>";
echo "<tr>";
echo "<td>".$row['work_order_no']."</td>";
echo "<td>".$row['product_no']."</td>";
echo "<td>".$row['ARBPL2']."</td>";
echo "<td>".$row['created_at']."</td>";
echo "<td>".$row['DDTEXT']."</td>";
echo "<td>".$obj->{'position'}."</td>";
echo "<td>".$row['ng_count']."</td>";
echo "</tr>";
//echo "<br>";
}
?>
我的$sql是從多個table撈進來的整理,不太適合用distinct
有沒有可以往array的方向去解?
提示:
group by
SELECT i1,i2,i3,i4,* FROM db GROUP BY i1,i2,i3,i4
如果還需要計數則用 count
SELECT i1,i2,i3,i4,count(*),* FROM db GROUP BY i1,i2,i3,i4
如果還需要特別的排序。則需要
SELECT i1,i2,i3,i4,count(*),* FROM (
SELECT * FROM db ORDER BY i1
) AS tempDB GROUP BY i1,i2,i3,i4