Jump to content

[SOLVED] How do I select only the lowest price for each unique ID?


Recommended Posts

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.

 

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.