In a table that has the columns: 'product_id', 'price',
... (and others). I need to obtain the record with the lowest and unique price for a given product_id.
I tried different sentences without having the expected result. Finally I came across this sentence (for product_id = 2
):
SELECT `price` , `product_id`
FROM bids
WHERE `product_id` = 2
GROUP BY `price`
HAVING COUNT( `price` ) = 1
ORDER BY `price`
LIMIT 1;
But it seems not to be an elegant solution having recourse to LIMIT
and ORDER
. How can I obtain the same record using the MIN()
function or even something else?