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
https://forums.phpfreaks.com/topic/220666-selecting-second-third-highest/
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.

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 />';

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.