Jump to content

Selecting the max value


unknown101

Recommended Posts

I have the following query:

 

highest_avg=mysql_query("SELECT * FROM track_tbl where Artist_ID='$get_artist_ID'")

 

This returns the following details:

 

Track_ID || Track || Album_ID || Artist_ID || Average_rating ||

    2              Blah          4                5                        9

  2              Blah1        4                5                        6

 

Now both tracks are the same artist, but I want to select the one with the hightest rating - so in this case track "Blah". Also keeping in mind that I cant order in Descending order and limit to 1 because the artist may have many tracks with the same ratings.  If multiple tracks have the same rating then i want to use a random function to select 1 record. Can anyone suggest how to do this?

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/101342-selecting-the-max-value/
Share on other sites

<?php
// Make a MySQL Connection

$query = "SELECT type, MAX(price) FROM products GROUP BY type"; 

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
echo "The most expensive  ". $row['type']. " is $" .$row['MAX(price)'];
echo "<br />";
}
?>

 

Here is a sample. This is found at: http://www.tizag.com/mysqlTutorial/mysqlmax.php

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.