我是sql的新手,并且一直在处理一些查询。
我正在尝试使用union并加入。
如果我不输出数字0,它会起作用。
如果有人可以指导我出错的地方。
SELECT m.movie_title, m.release_year,COUNT(c.actor_name) female_actors
GROUP BY m.movie_title, m.release_year
UNION
SELECT COUNT(c2.actor_name), 0
答案 0 :(得分:1)
SELECT m.movie_title, m.release_year,
SUM(case when a.gender='Female' then 1 else 0 end) female_actors
FROM MOVIE m LEFT JOIN CAST_MEMBER c on(m.movie_title=c.movie_title AND m.release_year=c.release_year) JOIN ACTOR a on(a.actor_name=c.actor_name)
GROUP BY m.movie_title, m.release_year
答案 1 :(得分:1)
你也必须外接ACTOR。内部联接删除所有外部联接的CAST_MEMBER,因为当前没有不存在的CAST_MEMBER的actor。这就是要做的一切。
SELECT m.movie_title, m.release_year, COUNT(c.actor_name) female_actors
FROM MOVIE m
LEFT JOIN CAST_MEMBER c on (m.movie_title = c.movie_title AND m.release_year = c.release_year)
LEFT JOIN ACTOR a on (a.actor_name = c.actor_name AND a.gender = 'Female')
GROUP BY m.movie_title, m.release_year;