The Chos3n Ron
The Chos3n Ron

Reputation: 5

Use ID from a table then use ID from that table to get data SQL

SELECT
    player.FirstName,
    player.LastName,
    team.teamName,
    TotalPoints,
    TotalRebounds,
    TotalAssists,
    TotalSteals,
    TotalBlocks
FROM playerstats
INNER JOIN player
    ON playerstats.PlayerID = player.PlayerID

I have a table called player (Player ID, firstname, lastname, team ID) table called Team (teamID, teamName). I have a table called playerstat(which has playerID)

I want to list firstname, last name and team along with total points, rebounds, etc

I want to use the playerID to get the team Id then list the team name ... if that makes sense. Not sure how to do that.

Upvotes: 0

Views: 249

Answers (2)

Binoy Kumar
Binoy Kumar

Reputation: 432

Select P.FirstName,P.LastName,t.teamName,s.TotalPoints 
from Player P
join Team t on P.TeamId=t.TeamId
join playerstats s on P.PlayerId=s.PlayerId

Upvotes: 0

Sloan Thrasher
Sloan Thrasher

Reputation: 5040

In your question, you don't indicate if the playerstats table has multiple rows per player, and you don't indicate what the team table's columns might be, but presuming that the playerstats table has multiple rows per player, and that you have a team table, you might give this a try. You will need to substitute the correct column names:

SELECT
    player.FirstName,
    player.LastName,
    team.teamName,
    sum(points) as TotalPoints,
    sum(rebounds) as TotalRebounds,
    sum(assists) as TotalAssists,
    sum(steals) as TotalSteals,
    sum(blocks) as TotalBlocks
FROM playerstats
INNER JOIN player
    ON playerstats.PlayerID = player.PlayerID
JOIN team
    ON team.id = player.teamID
GROUP BY player.FirstName, player.LastName,team.teamName
ORDER BY player.FirstName, player.LastName,team.teamName;

Upvotes: 0

Related Questions