Jump to content

How to Get Row results based on Min


dyluck

Recommended Posts

Hi there... I can't seem to find a php query that works for the min function.

 

I am trying get the entire row's contents/results from a table based on the minimum value of one of the columns.... everything I have tried returns errors.

 

Here is some of the things I have tried:

"SELECT * FROM AUTOPODUCT WHERE PNAME = '%s' AND MIN('SALEPRICE')"

"SELECT MIN('SALEPRICE') FROM AUTOPODUCT WHERE PNAME = '%s' GROUP BY PNAME"

 

hope you can help! :)

Link to comment
Share on other sites

It looks like you want the entire row for a particular product that has the lowest price.

 

One way to do this is to select the data sorted by price and only return one row:

SELECT * FROM AUTOPODUCT WHERE PNAME = '%s'  ORDER BY SALEPRICE LIMIT 1"

 

Another way to do it is using a subquery:

SELECT * FROM AUTOPODUCT WHERE PNAME = '%s' AND SALEPRICE =
(SELECT MIN(SALEPRICE) FROM AUTOPODUCT WHERE PNAME = '%s')"

 

If you have multiple rows with the same SALEPRICE (which is the lowest for the product) the first method will only return one of them and you will have no way to know that there is another.  The second method will return multiple rows and you can test the row count to see if there is more than one with that price.

Link to comment
Share on other sites

Thanks guys

Turns out with your help and the fact I spelled AUTOPRODUCT wrong i got it :)

 

"SELECT * FROM AUTOPODUCT WHERE PNAME = '%s'  ORDER BY SALEPRICE LIMIT 1"

Ended up being the one that worked.  The other one passed an error that said "too few arguments" and gave the SQL error.

Wonder why that is?

 

Link to comment
Share on other sites

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.