Jump to content

help with join


fnsweet

Recommended Posts

Hello everyone,

 

I am hopping someone can help me sort out a query.

 

I have two tables, one table is a players table, and a second table is a player_vitals data that stores heights and weight changes.  I want to list all the players showing the latest height and weight change.

 

this query will not fetch the latest height and weight

 

SELECT a.player_id, a.player_first_name,a.player_last_name, b.player_height, b.player_weight
FROM players a
LEFT JOIN (SELECT vital_id, player_id, player_height, player_weight FROM player_vitals ORDER BY vital_id DESC LIMIT 1) b ON b.player_id = a.player_id
LEFT JOIN teams c using (team_id)
ORDER BY player_last_name, player_first_name
 

 

this query will work only if i tell it what player_id to select from

 

SELECT a.player_id, a.player_first_name,a.player_last_name, b.player_height, b.player_weight
FROM players a
LEFT JOIN (SELECT vital_id, player_id, player_height, player_weight FROM player_vitals WHERE player_id = 6 ORDER BY vital_id DESC LIMIT 1) b ON b.player_id = a.player_id
LEFT JOIN teams c using (team_id)
where a.player_id = 6
ORDER BY player_last_name, player_first_name
 
but i need I need to get my first query to work listing all players, am i missing something?
 
Thanks
Link to comment
Share on other sites

Try

 

SELECT p.player_id, p.player_first_name, p.player_last_name,
       v.player_height, v.player_weight
FROM players p
LEFT JOIN player_vitals v
  ON p.player_id = v.player_id
 AND v.vital_id IN (SELECT MAX(vital_id) FROM player_vitals GROUP BY player_id)
LEFT JOIN teams c using (team_id)
ORDER BY p.player_last_name, p.player_first_name
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.