SQL查询:期望多个结果,但只返回一个

时间:2014-02-26 09:01:35

标签: sql

运行此查询后,我得到的结果数量有问题:

SELECT g.genre_name, 
       count(distinct(d.director_id))
  FROM genre g, 
       director d, 
       movie m
 WHERE g.genre_id=m.genre_id 
       AND m.director_id=d.director_id;   

表及其键是:

  1. genre(genre_id, genre_name)
  2. movie(genre_id, director_id)
  3. director(director_id)
  4. 有人可以帮忙吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

尝试添加GROUP BY

  SELECT g.genre_name, 
         count(distinct(d.director_id))
    FROM genre g, 
         director d, 
         movie m
   WHERE g.genre_id=m.genre_id 
         AND m.director_id=d.director_id
GROUP BY g.genre_name;