JOIN 具有多对多关系的多个表

发布于 2024-11-28 00:46:21 字数 762 浏览 0 评论 0原文

我有大约30张桌子。例如:与电影相关的制片人、导演等,

每个表由 movie_id 和 celeb_id 字段组成

,有一个表 celebs,其中包含以下字段: celeb_id celeb_name

对于特定 movie_id,我希望在单个查询中检索所有信息。

SELECT b_movies.*, bb_celebs.celeb_name 
FROM b_movies
LEFT JOIN b_producer ON b_movies.id = b_producer.movie_id 
LEFT JOIN bb_celebs ON b_producer.celeb_id = bb_celebs.celeb_id
LEFT JOIN b_director ON b_movies.id = b_director.movie_id 
LEFT JOIN bb_celebs ON b_director.celeb_id = bb_celebs.celeb_id
WHERE b_movies.id = 'UNIQUE ID';

我想要与工作类型相关联的名人的名字:

Director
1. ABC
2. XYZ

Producer
1. ABC2
2. XYZ2

所有名人的名字都存储在 celebs 表中,其中包含 celeb_id 和 celeb_name 在制片人表 movie_id 和 celeb_id 中,相同的结构适用于导演等。所有其他表

请帮忙。

问候,

I have around 30 tables. eg: producer, director etc related to movies

each table consists of movie_id and celeb_id field

there is a table celebs which consists of fields: celeb_id celeb_name

For a specific movie_id i want all the information retrieved in a single query.

SELECT b_movies.*, bb_celebs.celeb_name 
FROM b_movies
LEFT JOIN b_producer ON b_movies.id = b_producer.movie_id 
LEFT JOIN bb_celebs ON b_producer.celeb_id = bb_celebs.celeb_id
LEFT JOIN b_director ON b_movies.id = b_director.movie_id 
LEFT JOIN bb_celebs ON b_director.celeb_id = bb_celebs.celeb_id
WHERE b_movies.id = 'UNIQUE ID';

I want the names of celebs associated with the type of work:

Director
1. ABC
2. XYZ

Producer
1. ABC2
2. XYZ2

All names of celebs are stored in celebs table with celeb_id and celeb_name
and in producer table movie_id and celeb_id and same structure applies to directors, etc.. all other tables

Please help.

Regards,

Kenh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

终止放荡 2024-12-05 00:46:21
SELECT ...
FROM ...
LEFT JOIN ... ON ...
WHERE ...
ORDER BY ...

... 的适当值留作 OP 的练习...

SELECT ...
FROM ...
LEFT JOIN ... ON ...
WHERE ...
ORDER BY ...

Appropriate values for ... are left as an excercise to the OP...

幸福%小乖 2024-12-05 00:46:21

(select 'Director', celeb.name from celebs c joindirector d on c.celeb_id = d.celeb_id and d.movi​​e_id = "movie_id")

union all

(select 'Producer', celeb.name from celebs c join Producer p on c.celeb_id = p.celeb_id 和 p.movi​​e_id = "movie_id")

联合所有
……

(select 'Director', celeb.name from celebs c join director d on c.celeb_id = d.celeb_id and d.movie_id = "movie_id")

union all

(select 'Producer', celeb.name from celebs c join producer p on c.celeb_id = p.celeb_id and p.movie_id = "movie_id")

union all
.....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文