简体   繁体   English

SQL查询返回多个表中的特定属性的名称和计数

[英]SQL query return name and count of specific attributes across multiple tables

Given multiple tables I'm trying to write a query that returns the names that satisfies a specific count clause. 给定多个表我正在尝试编写一个返回满足特定count子句的名称的查询。

I have the tables: 我有桌子:

 genre(genre, movieid)
 moviedirectors(movieid, directorid)
 directors(directorid, firstname, lastname)

I want to write a query that returns the first and last name of directors that directed at least 50 movies of the genre comedy, and return that number as well. 我想写一个查询,返回导演至少50部类型喜剧电影的导演的名字和姓氏,并返回该数字。

This is what I have 这就是我所拥有的

select d.fname, d.lname, count(*)
from genre g, directors d, moviedirectors md
where g.genre='Comedy' and g.movieid=md.movieid and    
                  md.directorid=d.directorid
group by d.id
having count(*) >= 50

I believe this should be correct but when I run this query on the command line it never finishes. 我相信这应该是正确的,但是当我在命令行上运行此查询时,它永远不会完成。 I waited 30 minutes and got no results. 我等了30分钟,没有结果。

you need inner joins: 你需要内部联接:

SELECT d.fname
       d.lname
FROM genre g 
INNER JOIN moviedirectors md
    ON g.movieid = md.movieid
INNER JOIN directors d
    ON md.directorid = d.directorid
WHERE g.genre = 'Comedy' 
GROUP BY d.fname,        -- group by columns in select
         d.lname
HAVING COUNT(*) >= 50

选择c.firstname,c.lastname,count(e.movi​​eid)from(选择一个。*来自导演a,电影b,其中b.genre ='喜剧'和b.movi​​eid = a.movi​​eid)d,导演c其中c .directorid = d.directorid group by e.movi​​eid,计数(e.movi​​eid)> 50;

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

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