简体   繁体   中英

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.

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.

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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