Jump to content

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
https://forums.phpfreaks.com/topic/186939-how-to-get-row-results-based-on-min/
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.

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?

 

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.