Jump to content

Help with Join


Skipjackrick

Recommended Posts

I am trying to get the largest fish that each angler caught but also associate the correct data in the row.

 

With the following code I get an error message

column 'angler' is ambiguous

 

Do you know how to fix that?  I am still learning this join and on stuff.

select angler, 
       team_id, 
       species_id, 
       length, 
       image
  from submit
  join (select angler,
               max(length) as mlength
          from submit
         group by angler) dt
    on (submit.angler,submit.length) = (dt.angler,dt.mlength)
where submit.region_id = 1
order by submit.species_id

Link to comment
Share on other sites

angler is being returned by both the submit table and the subquery.  In order to fix that you just need to qualify the angler which is in the outermost select statement (i.e. dt.angler)

 

~judda

 

Thanks..

 

So I've rearranged it a little bit...It seems to work but its only returning 7 rows.  When there should be like 15 rows because there are more species than that.  Do you happen to know why it might be doing that?

 

<?php
      //Query for records region 1
      $records1 = "SELECT 
                       angler
                       ,team_id
                       ,dt.species_id
                       ,length
                       ,image
                       FROM submit
                         join (SELECT 
                                  species_id
                                  ,max(length) as mlength
                               FROM submit
                                GROUP BY species_id
                               ) dt
                           ON (submit.species_id,submit.length) = (dt.species_id,dt.mlength)
                        WHERE submit.region_id = 1
                        ORDER BY submit.species_id
                ";
      $records_result1 = mysql_query($records1) or die(mysql_error());
?>

Link to comment
Share on other sites

OH!!!

 

I get it...My WHERE statement should also be in the right table.  I hope that works.

 

Right table = the table on the right hand side of the join.

 

Left table = Submit

Right table = select angler,

              max(length) as mlength

          from submit

        group by angler

 

~judda

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.