How to get lowest value for 2 duplicate entries in PHP and MySQL
I ha开发者_C百科ve this table, that contains various products for a store. They each have an identifier, which is universal and distinct for every product, it is set by the manufacturer. But since I am getting the price lists from several wholesalers to get the lowest price possible, I have more than 1 row with the same identifier.
I would like to have a way to get the whole product list showing every product only once,but for the duplicate products show the lowest price possible. How to do this?
I thought of doing a SELECT on every product to check if it is the single on, but that would be inefficient. I have also thought about some array manipulations, but that too would add a lot of time.
Background info: I currently have about 30k products, which is planned to rise to about 100k soon.
A standard query for my product table looks like this:
SELECT productId,productIdentifier,productWholesaler,productPrice FROM `products`
The productIdentifier
is the part that has the same value for products from multiple wholesalers, but the productPrice
is never the same for 2 duplicates.
Try this:
SELECT productId, productIdentifier, productWholesaler, MIN(productPrice)
FROM `products`
GROUP BY productId, productIdentifier, productWholesaler
精彩评论