简体   繁体   English

SQL Server:连接三个表并返回空记录

[英]SQL Server: Join Three Tables and Return With Null Records

I don't know how to join this tree tables for the expected result. 我不知道如何加入此树表以获得预期的结果。 I was testing with LEFT / RIGHT / INNER / OUTER joins but i can get the row with the "null" value. 我正在用LEFT / RIGHT / INNER / OUTER联接进行测试,但是我可以获得带有“ null”值的行。

I was playing wiht this tables: 我在玩这张桌子:

Games Table: 游戏表:

Title    id
------------
Halo     1
Portal   2
Mario    3

Genres Table: 类型表:

Name     id
------------
Action   1
FPS      2
MMO      3
Arcade   4
Puzzle   5

Genres for games table: 游戏表的类型:

GameId     GenreId
------------------
1          1
1          2
3          5

This is what i get: 这就是我得到的:

Title       Genre
-----------------
Halo        Action
Halo        FPS
Mario      Puzzle

This is the result i want: 这是我想要的结果:

Title       Genre
-----------------
Halo        Action
Halo        FPS
Portal      NULL
Mario      Puzzle

Note: I read this question: SQL Server : Join Two Tables and Return With Null Records but no helps me. 注意:我读了这个问题: SQL Server:连接两个表并返回带有空记录的记录,但是没有帮助。

Please, some kind of help! 请帮忙!

You need left (outer) joins to do this: 您需要左(外)连接来执行此操作:

SELECT Games.Title, Genres.Name
FROM Games
LEFT JOIN GameGenres ON Games.Id = GameGenres.GameId
LEFT JOIN Genres ON GameGenres.GenreId = Genres.Id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM