Based on your example table, it appears you want to be grouping on product
rather than id
. You merely need to add the Size
column to both the SELECT
list and the GROUP BY
$query = "SELECT
product,
Size,
SUM(Quantity) AS TotalQuantity
FROM inventory
GROUP BY product, Size";
Note that I have added a column alias TotalQuantity
, which will allow you to more easily retrieve the column from the fetched row via the more sensible $row['TotalQuantity']
, rather than $row['SUM(Quantity)']
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…