Jump to content

Selecting second, third highest?


Rifts

Recommended Posts

hey all how would I do this.. I'm guessing I need to go four different SELECT queries

 

I have prices in my DB

 

I need to select the highest price one echo it

select the second highest price echo it

select third highest echo it

then select everything else and echo them

 

I have this so far but i dont know how to get the second, third, ext highest.

$result = mysql_query("SELECT * FROM stuff ORDER BY price DESC LIMIT 1");
while($row = mysql_fetch_array($result))
{
	echo $row['image'] . " " . $row['link'];
	echo "<br />";
}

 

Link to comment
Share on other sites

Right now you are ordering them by highest price first, followed by 2nd, third, fourth, etc.

 

So what you're doing will accomplish your goal just fine, except you're adding LIMIT 1 so it's only selecting 1 row, happening to be the highest, just do limit 10 or so, and it will order them by highest price first.

Link to comment
Share on other sites

Yes, you can.

 

$result = mysql_query("SELECT * FROM stuff ORDER BY price DESC LIMIT 1");
while($row = mysql_fetch_array($result)) {
echo $row['image'] . " " . $row['link'];
echo "<br />";
$prices[] = $row['price'];
}
rsort($prices,SORT_NUMERIC);

echo 'Highest price: $' . $prices[0] . '<br />';
echo 'Third Highest Price: $' . $prices[2] . '<br />';

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.