I am trying to get the minimum price (totalprice) from a grouped column (sku) and then get the corresponding supplier data (supplier) associated with the resulting lowest price.
Table example
sku supplier totalprice
505 Sup1 20
505 Sup2 30
505 Sup3 25
605 Sup1 100
605 Sup2 97
605 Sup3 111
I am trying to group the sku column and get the lowest price and related supplier,
$query = "SELECT sku, supplier, MIN(totalprice) FROM pricetable GROUP BY sku";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result)) {
echo "The cheapest ". $row['sku']. " is £" .$row['MIN(totalprice)']." from ".$row['supplier'];
For example,
group 505 has the lowest price of 20 from supplier Sup1
group 606 has the lowest price of 97 from supplier Sup2
The code above gives me the lowest price for each sku group, but I can't get the associated supplier name with it.