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
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

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.