简体   繁体   English

选择特定工作室的工作室,演员和电影数量

[英]Select sdudios, actors and amount of films produced in a certain studio

I continue experements with MySQL and have next. 我将继续使用MySQL进行实验,然后进行下一步。 I need to select names of actors who starred in the films a certain studio. 我需要选择在某个工作室出演电影的演员的名字。 The selection includes the amount of films of the studio, which starred actor. 选择内容包括出演演员的工作室电影数量。 The result is represented in the form of the table with the following columns: "Studio," "Actors", "Number of films". 结果以表格的形式显示,其中包含以下列:“工作室”,“演员”,“电影数量”。 I have three table: actors, studios and films. 我有三张桌子:演员,工作室和电影。 actors and films have many-to-many, studios and film have many-to-many relationship. 演员和电影有多对多关系,电影制片厂和电影有多对多关系。 Actors and studios haven't relationship. 演员和工作室没有关系。 So I can select studios, actors and films. 因此,我可以选择工作室,演员和电影。

SELECT studios.title, CONCAT_WS(' ', actors.name, actors.surname), films.title 
FROM studios
JOIN studios_films ON studios.id = studios_films.studio_id
JOIN films ON films.id = studios_films.film_id
JOIN films_actors ON films.id = films_actors.film_id
JOIN actors ON actors.id = films_actors.actor_id 

But how can I get not title of film but amount of films of the studio, which starred actor. 但是我怎么能得到的不是电影的片名,而是演员出演的工作室的电影数量。 I think I have to use aggregate (GROUP BY) Function COUNT. 我想我必须使用聚合(GROUP BY)函数COUNT。 But when I use it in my query 但是当我在查询中使用它时

SELECT studios.title, CONCAT_WS(' ', actors.name, actors.surname), COUNT(films.id) 
FROM studios
JOIN studios_films ON studios.id = studios_films.studio_id
JOIN films ON films.id = studios_films.film_id
JOIN films_actors ON films.id = films_actors.film_id
JOIN actors ON actors.id = films_actors.actor_id
GROUP BY filmstudios.id 

I don't get, what I want. 我不明白我想要什么。 Can You help me to create query?Thanks! 您能帮我创建查询吗?谢谢!

You can use the GROUP BY clause with multiple columns. 您可以将GROUP BY子句与多列一起使用。 This way you can count the number of films for every studio / actor combination. 这样,您可以计算每个工作室/演员组合的电影数量。

Your complete query would look like this: 您的完整查询如下所示:

SELECT studios.title, CONCAT_WS(' ', actors.name, actors.surname), COUNT(films.id) 
FROM studios
JOIN studios_films ON studios.id = studios_films.studio_id
JOIN films ON films.id = studios_films.film_id
JOIN films_actors ON films.id = films_actors.film_id
JOIN actors ON actors.id = films_actors.actor_id
GROUP BY studios.id, actors.id

You can read more about multiple columns in GROUP BY clauses here: 您可以在此处的GROUP BY子句中了解有关多个列的更多信息:

Question on SO: Using group by on multiple columns 关于SO的问题: 在多个列上使用分组依据

Tutorial: http://dba.fyicenter.com/faq/mysql/Use-Multiple-Columns-in-GROUP-BY.html 教程: http//dba.fyicenter.com/faq/mysql/Use-Multiple-Columns-in-GROUP-BY.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM