Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Query from 3 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, let's suppose that i want all the Winner records referring to the Player records referring to The Game with the ID=2, how can i proceed?

Thank you.

like image 594
Slimane Agouram Avatar asked Aug 06 '13 13:08

Slimane Agouram


2 Answers

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

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
like image 106
Darren Avatar answered Oct 02 '22 05:10

Darren


you have specified all the Winner records So that i have used the left join for player and game. But the Overall code works according to the where condition.

Try This,

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
like image 31
Selva Avatar answered Oct 02 '22 05:10

Selva