Jump to content

MySQL MAX() usage


Skipjackrick

Recommended Posts

For some reason I am only getting one result when I use the following code.

 

Does anybody know what I am doing wrong?

 

<?php
$querySC = "SELECT 
              species_id, 
              team_id, 
              angler, 
              MAX(length), 
              image
            FROM
              submit
            WHERE
              region_id=1
            GROUP BY
              species_id
           ";

$resultSC = mysql_query($querySC) or die (mysql_error());



if (mysql_num_rows($resultSC) > 0){

while($row = mysql_fetch_array($resultSC))
{
$species_id = $row['species_id'];
$angler = $row['angler'];
$team_id = $row['team_id'];
$length = $row['MAX(length)'];
$image = $row['image'];

//get team's name from team table
get_team_name($team_id);
get_species($species_id);
get_angler_handle($angler);
}
$recordsSC .=<<<EOD
<tr>
	<td align='center'><a href='submitted_pics/$image' onclick='return hs.expand(this)' class='highslide' title='Click here to see the $speciesname'>$speciesname</a></td>
	<td align='center'><a href='anglerprofile.php?anglerid=$angler' title='Find out more about $anglerhandle'>$anglerhandle</a></td>
	<td align='center'><a href='teampage.php?team_idvar=$team_id&team=$teamname' title='Find out more about $teamname'>$teamname</a></td>
	<td align='center'>$length"</td>
</tr>
EOD;

} else {
   echo "";
}

$bonus_footer ="</table>";

$bonus_wars =<<<BONUS
	$bonus_header
	$recordsSC
	$bonus_footer
BONUS;

print $bonus_wars;
?>

Link to comment
Share on other sites

Your code that is building $recordsSC is after and outside of your while(){} loop that is retrieving the data, so of course it will only get the last value retrieved. You would need to put that code inside of the while(){} loop for it to do something with each row that is being retrieved.

 

The $length variable should hold the maximum value for each GROUP BY species_id, but you are aware that is only going to give you the maximum length in each group, the other column values are actually gong to be from the first row in each group. If you are actually trying to get the row in each group that has the the maximum length, you will need to use the techniques at this link - http://dev.mysql.com/doc/refman/5.1/en/example-maximum-column-group-row.html

Link to comment
Share on other sites

??  THanks... but still not working for me.  But I think I am moving forward.

 

Here is what I've got.

 

I am not sure what the s1 and s2 are meant for but put them in there???  Any help explaining?

 

<?php
$querySC = "SELECT 
              species_id, 
              team_id, 
              angler, 
              length, 
              image
            FROM
              submit s1
            WHERE
              region_id=1 AND length=(SELECT MAX(s2.length) FROM submit s2 WHERE s1.species_id = s2.species_id)";
           ";

$resultSC = mysql_query($querySC) or die (mysql_error());



if (mysql_num_rows($resultSC) > 0){

while($row = mysql_fetch_array($resultSC))
{
$species_id = $row['species_id'];
$angler = $row['angler'];
$team_id = $row['team_id'];
$length = $row['length'];
$image = $row['image'];

//get team's name from team table
get_team_name($team_id);
get_species($species_id);
get_angler_handle($angler);

$recordsSC .=<<<EOD
<tr>
	<td align='center'><a href='submitted_pics/$image' onclick='return hs.expand(this)' class='highslide' title='Click here to see the $speciesname'>$speciesname</a></td>
	<td align='center'><a href='anglerprofile.php?anglerid=$angler' title='Find out more about $anglerhandle'>$anglerhandle</a></td>
	<td align='center'><a href='teampage.php?team_idvar=$team_id&team=$teamname' title='Find out more about $teamname'>$teamname</a></td>
	<td align='center'>$length"</td>
	<td align='center'><a href='submitted_pics/$image' onclick='return hs.expand(this)' class='highslide' title='Click here to see the Fish'>Image</a></td>
</tr>
EOD;
}
} else {
   echo "";
}

$bonus_footer ="</table>";

$bonus_wars =<<<BONUS
	$bonus_header
	$recordsSC
	$bonus_footer
BONUS;

print $bonus_wars;
?>

Link to comment
Share on other sites

Define: but still not working for me. What is it doing vs what you expect? We are not standing right next to you nor do we have access to your server, your database, or your complete code so you must state what you see in front of you if you expect someone in a forum to be able to help you.

 

Based on the color highlighting in the posted code, you have a php syntax error due to an extra "; in the code.

Link to comment
Share on other sites

Sorry...sometimes i don't realize that you guys can't see what I see... I bet that can be irritating...LOL

 

Ok...

 

So it seems to be working sort of...

 

I am now getting more than one row to show up.  However only 2 anglers are showing up.

 

Here is what is output

 

Species--------------Angler--------------Team--------------Length--------------Image

Red Drum--------PierPressure-------Hawg Wild------------51.00" --------------Image

Yellowfin Tuna--------Oz--------------Rockstar--------------44.00" --------------Image

Cobia (Ling)-----------Oz--------------Rockstar--------------53.00" --------------Image

King Mackerel---------Oz--------------Rockstar--------------55.00" --------------Image

Trout--------------------Oz--------------Rockstar--------------31.50" --------------Image

 

 

However there are several more MAX length with different teams and anglers that aren't being displayed....??

<?php
$querySC = "SELECT 
              species_id, 
              team_id, 
              angler, 
              length, 
              image
            FROM
              submit s1
            WHERE
              region_id=1 AND length=(SELECT MAX(s2.length) FROM submit s2 WHERE s1.species_id = s2.species_id)
           ";

$resultSC = mysql_query($querySC) or die (mysql_error());



if (mysql_num_rows($resultSC) > 0){

while($row = mysql_fetch_array($resultSC))
{
   $species_id = $row['species_id'];
   $angler = $row['angler'];
   $team_id = $row['team_id'];
   $length = $row['length'];
   $image = $row['image'];

//get team's name from team table
   get_team_name($team_id);
   get_species($species_id);
   get_angler_handle($angler);

$recordsSC .=<<<EOD
<tr>
      <td align='center'><a href='submitted_pics/$image' onclick='return hs.expand(this)' class='highslide' title='Click here to see the $speciesname'>$speciesname</a></td>
      <td align='center'><a href='anglerprofile.php?anglerid=$angler' title='Find out more about $anglerhandle'>$anglerhandle</a></td>
      <td align='center'><a href='teampage.php?team_idvar=$team_id&team=$teamname' title='Find out more about $teamname'>$teamname</a></td>
      <td align='center'>$length"</td>
      <td align='center'><a href='submitted_pics/$image' onclick='return hs.expand(this)' class='highslide' title='Click here to see the Fish'>Image</a></td>
   </tr>
EOD;
}
} else {
   echo "";
}

$bonus_footer ="</table>";

$bonus_wars =<<<BONUS
      $bonus_header
      $recordsSC
      $bonus_footer
BONUS;

   print $bonus_wars;
?>

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.