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
https://forums.phpfreaks.com/topic/292063-help-with-join/
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
https://forums.phpfreaks.com/topic/292063-help-with-join/#findComment-1494831
Share on other sites

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.