0

I'm trying to access a piece of data from another table. I'm trying to reach GameName field but I don't really know how to do this.

I tried to create another procedure but this didn't really work out.

PlayerGamesList.sql

...
SELECT
    t.PlayerId,
    t.GameId,
    t.TeamId,
    t.Position
FROM
    [PlayerOnGame] t

PlayerOnGame.sql

CREATE TABLE [dbo].[PlayerOnGame]
(
    [PlayerOnGameId] BIGINT PRIMARY KEY IDENTITY NOT NULL,
    [PlayerId] BIGINT NOT NULL,
    [GameId] BIGINT NULL,
    [TeamId] BIGINT NOT NULL,
    [Position] NVARCHAR(100) NULL,
...
)

and Game.sql:

CREATE TABLE [dbo].[Game] 
(
    [GameId] BIGINT PRIMARY KEY IDENTITY NOT NULL,
    [Name]   NVARCHAR(100) NOT NULL,
    ...
)

How can I access this specific GameId? I mean how can I do something like GameId.Name?

Dale K
  • 25,246
  • 15
  • 42
  • 71
Andrei Rk
  • 3
  • 1
  • If you are asking how to fetch rows from both tables based on GameId then you need an `INNER JOIN` : https://www.w3resource.com/sql/joins/perform-an-inner-join.php – Alex K. Aug 06 '19 at 11:03
  • Joint the tables you want the data from – Himanshu Aug 06 '19 at 11:04
  • read this to look on joins in sql server https://stackoverflow.com/questions/17946221/sql-join-and-different-types-of-joins – Hardik Leuwa Aug 06 '19 at 11:58

1 Answers1

1

As in, you don't want to see the game id, you want to see the name:

SELECT
    t.PlayerId,
    g.Name,
    t.TeamId,
    t.Position
FROM
    [PlayerOnGame] t
    INNER JOIN [Game] g ON g.GameId = t.GameId

Note though that you've said that [PlayerOnGame].[Name] is allowed to be null, which means you have the potential that some [PlayerOnGame] records don't have a [GameId] set. INNER JOIN will cause these rows to disappear from the results. If you want to keep them and show a null for the name, make it a LEFT JOIN instead of an INNER JOIN

Caius Jard
  • 72,509
  • 5
  • 49
  • 80