Question

I have three tables :

videogames.cod - playerid (PK), forename, surname, points
videogames.codhost - hostid (PK), playerid (FK), gameid (FK), wins (integer), losses (integer)
videogames.codgame - gameid (PK), gametype (extinction, deathmatch) 

I have created a stored procedure that returns the number of points scored by a player based on their surname.

CREATE PROCEDURE getpoints @surname nvarchar(30)
AS
SELECT forename, surname, points
FROM videogames.cod
WHERE surname = @surname
GO

The problem is I also want data from two other tables. I need the wins and losses from the codhost table, which are both integers, and I want the gametype from the codgame table i.e.

Forename, surname, points, wins, losses, gametype.

I know I need a join but I don't know what to join the tables on as they don't all share the same key. Yes I know I could change the fields in the tables but I would prefer to learn how to do this for future references and it makes more sense to me when I use things I understand - like games for example. Iknow it's dumb but it's how I learn.

Was it helpful?

Solution

try this, it's off the top of my head so if it doesn't work, I'll verify and modify:

CREATE PROCEDURE getpoints @surname nvarchar(30)
AS
SELECT c.forename, c.surname, c.points, h.wins, h.losses, g.gametype
FROM videogames.cod c
left join videogames.codhost h on c.playerid = h.playerid
left join videogames.codgame g on h.gameid = g.gameid
WHERE c.surname = @surname
GO
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top