Choose "Query" from three tables with foreign keys

I have 3 tables with foreign keys to each other. I want to write a SQL Server stored procedure to select records from one of them.

My tables

Now suppose I want all Winner entries to refer to Player entries that link to The Game with ID = 2, how can I continue?

Thanks.

+6
source share
2 answers

You have specified all Winner entries . So, I used the left connection for the player and the game. But the general code works in accordance with the terms of the condition.

Try it,

select w.* from Winner w left Join Player p on p.ID_player = w.player_FK left join Game g on g.ID_game = p.Game_FK where Game.ID_game = 2 
+6
source

You need to use SELECT and INNER JOIN , then you can use the WHERE to filter on GameID 2.

 SELECT ID_Winner, Name, Lastname, Player_FK FROM Winner INNER JOIN Player on Player.ID_Pplayer = Winner.Player_FK INNER JOIN Game ON Game.ID_game = Player.Game_FK WHERE Game.ID_game = 2 
+2
source

Source: https://habr.com/ru/post/951132/


All Articles