I have the following table:
Date | Product Number | Quantity | Amount |
---|---|---|---|
1-1-2022 | 308306 | 5 | $157.70 |
1-3-2022 | 308309 | 10 | $315.40 |
1-3-2022 | 401190 | 1 | $13.48 |
1-4-2022 | 401190 | 5 | $67.40 |
I'm looking to combine the rows by product number and count the quantity and sum the total amount.
Here is my query, which I think does what I want based on looking at examples on this site:
$query = "SELECT * FROM orders";
$result = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($result, MYSQLI_NUM))
{
$prodnum = $row[1];
$query2 = "SELECT * FROM products WHERE product_number = '$prodnum'";
$result2 = mysqli_query($dbc, $query2);
$row2 = mysqli_fetch_array($result2, MYSQLI_NUM);
$query3 = "SELECT product_number, COUNT(*), SUM(total_amount) FROM orders GROUP BY product_number";
$result3 = mysqli_query($dbc, $query3);
$row3 = mysqli_fetch_array($result3, MYSQLI_NUM);
$counter = $counter + 1;
echo "
<tr>
<td> $row[1] </td>
<td> $row2[2] </td>
<td> $row3[2] </td>
<td> $row3[2] </td>
</tr>
";
}
I would expect the following to be output:
Prod Num | Description | Count | Total Amount |
---|---|---|---|
308306 | BROWNIE, CHOC CRML SALTD | 15 | 473.10 |
401190 | MUSTARD, YLW SQZ BTL SHLF | 6 | 80.88 |
The problem is that all I get is the following:
Prod Num | Description | Count | Total Amount |
---|---|---|---|
308306 | BROWNIE, CHOC CRML SALTD | 1 | 24.05 |
401190 | MUSTARD, YLW SQZ BTL SHLF | 1 | 24.05 |
760785 | SPICE, PPR BLK GRND JUG REST | 1 | 24.05 |
Any help is appreciated.
Thank you.