简体   繁体   English

如何使用第三个表SQL Server从两个相关的表中检索数据

[英]How to retrieve data from two tables related using a third table, SQL Server

I have three tables(simplified) 我有三张桌子(简体)

movie(id int primary key identity, title varchar(20) not null)
genre(id int primary key identity, type varchar(10) not null)

movie_genre(movie_id int references movie(id), 
            genre_id int references genre(id), 
            primary key(movie_id, genre_id))

Data in movie 电影中的数据

id          title
---------------------------
1     |     Inception
2     |     The Dark Knight

Data in genre 数据类型

id          type 
---------------------
1     |     action 
2     |     adventure 
3     |     thriller

Data in movie_genre movie_genre中的数据

movie_id          genre_id 
----------------------------
1               |     1 
1               |     2 
2               |     1 
2               |     3 

I want to display movie name with its genre types displayed in one column. 我想显示电影名称,并将其流派类型显示在一栏中。 So, the output would be 因此,输出将是

title              |     genres 
-----------------------------------------
Inception          |     action adventure 
The Dark Knight    |     action thriller

I tried to do it in this way 我试图用这种方式

select 
    movie.title, genre.type 
from 
    movie, genre 
where 
    movie.id = movie_genre.movie_id 
    and genre.id = movie_genre.genre_id;

but it says : 但它说:

The multi-part identifier "movie_genre.movie_id" could not be bound. 无法绑定多部分标识符“ movie_genre.movi​​e_id”。
The multi-part identifier "movie_genre.genre_id" could not be bound. 不能绑定多部分标识符“ movie_genre.genre_id”。

I am very new to SQL, any help would be appreciated. 我对SQL非常陌生,将不胜感激。

Edit : 编辑:
Using 运用

SELECT G.[Type] ,M.[Title]
FROM movie_genre MG  
LEFT JOIN genre G ON MG.genre_id = G.ID
LEFT JOIN movie M ON MG.Movie_ID = M.ID

OR 要么

select movie.title, genre.type 
from movie, genre, movie_genre
where
movie.id = movie_genre.movie_id 
and genre.id = movie_genre.genre_id;

The output is now, 现在的输出是

    title              |     genres 
-----------------------------------------
Inception          |     action
Inception          |     adventure 
The Dark Knight    |     action
The Dark Knight    |     thriller

How could I display genres in one row? 如何在一行中显示流派?

SELECT G.[Type] 
       ,M.[Title]
FROM movie_genre MG  
LEFT JOIN genre G ON MG.genre_id = G.ID
LEFT JOIN movie M ON MG.Movie_ID = M.ID

To get a list 取得清单

SELECT DISTINCT M.[Title]
      ,STUFF((
           SELECT ' ' + G.[Type] 
           FROM genre G INNER JOIN movie_genre MG
           ON  MG.genre_id = G.ID
           WHERE MG.Movie_id = Mov.Movie_id
           FOR XML PATH(''),TYPE)
            .value('.','NVARCHAR(MAX)'),1,1, '') Genre
FROM movie_genre Mov  
INNER JOIN movie M ON Mov.Movie_ID = M.ID

OR 要么

SELECT DISTINCT M.[Title]
      ,STUFF(List,1,1, '') Genre
FROM @movie_genre Mov  
INNER JOIN @movie M 
ON Mov.Movie_ID = M.ID
             CROSS APPLY 
                       (
                       SELECT ' ' + G.[Type] 
                       FROM @genre G INNER JOIN @movie_genre MG
                       ON  MG.genre_id = G.ID
                       WHERE MG.Movie_id = Mov.Movie_id
                       FOR XML PATH('')
                       )Gen(List)

SQL FIDDLE

我相信您需要在FROM中添加“ movie_genre”,例如:

SELECT movie.title, genre.type FROM (movie, genre, movie_genre) WHERE ....

暂无
暂无

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

相关问题 如何从两个临时表中获取数据并插入第三个表而不在 SQL 服务器中添加新行 - How to get data from two temporary tables and insert in third table without adding new row in SQL Server SQL Server查询以从具有多个条件的两个表中检索数据 - SQL Server query to retrieve data from two tables with multiple conditions SQL Server-直接连接两个表,有时甚至通过第三个表连接 - SQL Server - Joining two tables directly or, sometimes, through a third table 如何使用 SQL Server Management Studio 连接相关表中的数据? - How do I concatenate data from related tables using SQL Server Management Studio? 合并SQL Server第三个表中的两个表的差异 - Merge difference of two tables in third table in SQL Server 将两个 SQL 服务器日志表组合成第三个活动日志表 - Combining two SQL Server log tables into a third activity log table 从最后购买的两个不同的SQL Server表中生成SSRS中的派生表,然后与第三个表连接 - Generating a derived table in SSRS from last purchases in two different SQL Server tables, and then joining with a third 从两个表sql中检索数据 - Retrieve data from two tables sql 如何从SQL Server中的多个每日表中检索数据 - How to retrieve data from multiple daily tables in SQL Server 如何使用包含参考值的第三个表将sql表拆分为两个表 - How to split a sql table into two tables using a third table containing reference values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM