从两个链接的多对多关系中检索数据

发布于 2025-01-04 09:13:50 字数 1063 浏览 3 评论 0原文

我正在练习我创建的一个示例,如下所示(其中我正在考虑一部电影能够属于许多类别,而一个类别能够属于许多子类别)。

不确定链接两个多对多关系是否正确(我将它们分成几个一对多关系),但是当我进行查询时,我得到了非常奇怪的结果。

有什么线索我如何获得所有电影的电影标题和流派名称,即使是那些没有流派名称的电影?我正在使用与此类似的查询,但它不会返回这些标题: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2025-01-11 09:13:50

让我提出示例数据,看看我是否正确:

  • 电影:“电锯惊魂”、“暮光之城”、“野战排”
  • 类型:“惊悚片”、“喜剧”、“动作”
  • 子类型:“好莱坞”、“法国”、“意大利”

如果这张图片是正确的,那么您的设计可能是错误的,因为电影和子类型之间没有关系。说了这么多,试试这个

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) 
LEFT OUTER JOIN genre_subgenre 
ON (genre_subgenre.genre_id = genre.genre_id) 
LEFT OUTER JOIN subgenre 
ON (subgenre.subgenre_id = genre_subgenre.subgenre_id) 

Let me propose sample data to see if I get it right:

  • Movies: "Saw", "Twilight", "Platoon"
  • Genre: "Thriller", "Comedy", "Action"
  • Subgenre: "Hollywood", "French", "Italian"

If this picture is correct then your design may be wrong as there is no relationship between Movie and Subgenre. That told, try this

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) 
LEFT OUTER JOIN genre_subgenre 
ON (genre_subgenre.genre_id = genre.genre_id) 
LEFT OUTER JOIN subgenre 
ON (subgenre.subgenre_id = genre_subgenre.subgenre_id) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文