daydreamer Posted November 8, 2009 Share Posted November 8, 2009 Hi, I have a table: ---tblPrices--- ID, Price, Product ID 1 22 106 2 25 106 3 19 106 4 88 107 5 77 107 I want to select only the lowest price for each unique product ID in one SQL call, how do I do it?! Thanks. Link to comment https://forums.phpfreaks.com/topic/180787-solved-how-do-i-select-only-the-lowest-price-for-each-unique-id/ Share on other sites More sharing options...
corbin Posted November 8, 2009 Share Posted November 8, 2009 You could use a group by clause along with an order. SELECT ID, MIN(minPrice) FROM tblPrices GROUP BY ID Link to comment https://forums.phpfreaks.com/topic/180787-solved-how-do-i-select-only-the-lowest-price-for-each-unique-id/#findComment-953854 Share on other sites More sharing options...
Alex Posted November 9, 2009 Share Posted November 9, 2009 I think you mean: SELECT ID, MIN(Price) FROM tblPrices GROUP BY Product ID Link to comment https://forums.phpfreaks.com/topic/180787-solved-how-do-i-select-only-the-lowest-price-for-each-unique-id/#findComment-953883 Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2009 Share Posted November 9, 2009 And if you want the actual rows with the minimum in each group (not just the minimum value in a group) you will need to use one of the methods at the following link (alter the code to use the MIN() function as needed) - http://dev.mysql.com/doc/refman/5.1/en/example-maximum-column-group-row.html Link to comment https://forums.phpfreaks.com/topic/180787-solved-how-do-i-select-only-the-lowest-price-for-each-unique-id/#findComment-953890 Share on other sites More sharing options...
daydreamer Posted November 9, 2009 Author Share Posted November 9, 2009 Thanks for the solutions, my actual SQL had a left join in it so the normal SELECT ID, MIN(Price) FROM tblPrices GROUP BY Product ID wasnt working. And if you want the actual rows with the minimum in each group (not just the minimum value in a group) you will need to use one of the methods at the following link (alter the code to use the MIN() function as needed) - http://dev.mysql.com/doc/refman/5.1/en/example-maximum-column-group-row.html This sorted it, Cheers Link to comment https://forums.phpfreaks.com/topic/180787-solved-how-do-i-select-only-the-lowest-price-for-each-unique-id/#findComment-953897 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.