从两个链接的多对多关系中检索数据
我正在练习我创建的一个示例,如下所示(其中我正在考虑一部电影能够属于许多类别,而一个类别能够属于许多子类别)。
不确定链接两个多对多关系是否正确(我将它们分成几个一对多关系),但是当我进行查询时,我得到了非常奇怪的结果。
有什么线索我如何获得所有电影的电影标题和流派名称,即使是那些没有流派名称的电影?我正在使用与此类似的查询,但它不会返回这些标题:S
如果我在第一个多对多关系之后停止,结果似乎是正确的,但一旦我添加第二个关系(使用下面的查询),我不没有收到任何东西...有什么建议吗?
SELECT movie.title, genre.name
FROM movie
LEFT OUTER JOIN movie_genre
ON (movie.movie_id = movie_genre.movie_id)
JOIN genre
ON (genre.genre_id = movie_genre.genre_id)
JOIN genre_subgenre
ON (genre_subgenre.genre_id = genre.genre_id)
JOIN subgenre
ON (subgenre.subgenre_id = genre_subgenre.subgenre_id)
+++++++++++++++++
+ Movie ID (PK) +
+ Movie Title +
+++++++++++++++++
|
|
|
+++++++++++++++++
+ Movie ID (FK) +
+ Genre ID (FK) +
+++++++++++++++++
|
|
|
+++++++++++++++++++
+ Genre ID (PK) +
+ Genre Name +
+++++++++++++++++++
|
|
|
++++++++++++++++++++
+ Genre ID (FK) +
+ Subgenre ID (FK) +
++++++++++++++++++++
|
|
|
+++++++++++++++++++++++++
+ Subrenre ID (PK) +
+ Subgenre Name (FK) +
+++++++++++++++++++++++++
I was practising with an example that I was creating like the one below (where I was thinking about a movie being able to belong to many categories and a category being able to belong to have many subcategories).
Not sure if chaining two many-to-many relationships is correct (I split them up in several one to many relationships), but when I'm making a query I'm getting pretty weird results.
Any clue how could I get movie title and genre name for all movies, even the ones without genre name? I'm using a query similar to this but it's not returning those titles :S
If I stop after the 1st many-to-many relationship, the results seem correct but once I add the 2nd relationship (with the query below), I don't receive anything... any suggestion pls?
SELECT movie.title, genre.name
FROM movie
LEFT OUTER JOIN movie_genre
ON (movie.movie_id = movie_genre.movie_id)
JOIN genre
ON (genre.genre_id = movie_genre.genre_id)
JOIN genre_subgenre
ON (genre_subgenre.genre_id = genre.genre_id)
JOIN subgenre
ON (subgenre.subgenre_id = genre_subgenre.subgenre_id)
+++++++++++++++++
+ Movie ID (PK) +
+ Movie Title +
+++++++++++++++++
|
|
|
+++++++++++++++++
+ Movie ID (FK) +
+ Genre ID (FK) +
+++++++++++++++++
|
|
|
+++++++++++++++++++
+ Genre ID (PK) +
+ Genre Name +
+++++++++++++++++++
|
|
|
++++++++++++++++++++
+ Genre ID (FK) +
+ Subgenre ID (FK) +
++++++++++++++++++++
|
|
|
+++++++++++++++++++++++++
+ Subrenre ID (PK) +
+ Subgenre Name (FK) +
+++++++++++++++++++++++++
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我提出示例数据,看看我是否正确:
如果这张图片是正确的,那么您的设计可能是错误的,因为电影和子类型之间没有关系。说了这么多,试试这个
Let me propose sample data to see if I get it right:
If this picture is correct then your design may be wrong as there is no relationship between Movie and Subgenre. That told, try this