Mysql Inner Join表继承同一个表

时间:2014-12-23 13:09:11

标签: mysql join inner-join

我有以下数据库架构。表VHS和DVD都继承自Suport_Media并且都包含一部电影(电影翻译成英文意思是电影)。我想创建一个视图,其中包含有关所有可用的Suport_Media及其包含的电影的信息。要做到这一点,我必须将表suport_media,dvd,vhs和film组合成一个带有内连接的表。问题是我无法弄清楚如何将VHS和DVD合并到一个表中,然后将它与Suport_Media和Film结合起来。

我尝试过以下代码:

CREATE
VIEW Informatii_Suport_Media AS
SELECT Titlu, Actori_Principali, Tip, Durata_Film, Data_Lansare, Pret
FROM VHS V
INNER JOIN DVD D ON V.Suport_Media_ID <> D.Suport_Media_ID
INNER JOIN DVD D1 ON Suport_Media.Suport_Media_ID = D.Suport_Media_ID
INNER JOIN Film F ON F.Film_ID = D1.Film_ID
GROUP BY V.Suport_Media_ID
ORDER BY V.Suport_Media_ID ASC;

架构位于: https://imageshack.com/i/hl0DNwc1p

1 个答案:

答案 0 :(得分:1)

DVDVHS分别加入其他表格,然后将其与UNION

合并
CREATE VIEW Informatii_Suport_Media AS
SELECT Titlu, Actori_Principali, Tip, Durata_Film, Data_Lansare, Pret
FROM DVD D
INNER JOIN Support_Media S ON S.Suport_Media_ID = D.Suport_Media_ID
INNER JOIN Film F ON F.Film_ID = D.Film_ID
GROUP BY S.Suport_Media_ID
UNION
SELECT Titlu, Actori_Principali, Tip, Durata_Film, Data_Lansare, Pret
FROM VHS V
INNER JOIN Support_Media S ON S.Suport_Media_ID = V.Suport_Media_ID
INNER JOIN Film F ON F.Film_ID = V.Film_ID
GROUP BY S.Suport_Media_ID

绕过限制将子查询放在视图的FROM子句中的另一种方法是创建另一个视图:

CREATE VIEW DVD_VHS AS
SELECT Support_Media_ID, Film_ID
FROM DVD
UNION
SELECT Support_Media_ID, Film_ID
FROM VHS;

CREATE VIEW Informatii_Suport_Media AS
SELECT Titlu, Actori_Principali, Tip, Durata_Film, Data_Lansare, Pret
FROM DVD_VHS DV
INNER JOIN Support_Media S ON S.Suport_Media_ID = DV.Suport_Media_ID
INNER JOIN Film F ON F.Film_ID = DV.Film_ID
GROUP BY S.Suport_Media_ID
ORDER BY S.Support_Media_ID ASC