SQLZOO: More JOIN operations/zh--用子查询创建一个临时表时要记得给这个表命名

12. 尊·特拉華達'John Travolta'最忙是哪一年? 顯示年份和該年的電影數目。

SELECT yr,COUNT(title) FROM
  movie JOIN casting ON movie.id=movieid
         JOIN actor   ON actorid=actor.id
where name='John Travolta'
GROUP BY yr
HAVING COUNT(title)=(SELECT MAX(c) FROM
(SELECT yr,COUNT(title) AS c FROM
   movie JOIN casting ON movie.id=movieid
         JOIN actor   ON actorid=actor.id
 where name='John Travolta'
 GROUP BY yr) AS t
)

注意最后的AS t,没有这个是不行的

13. 列出演員茱莉·安德絲'Julie Andrews'曾參與的電影名稱及其第1主角。

select title,name
from movie JOIN casting ON movie.id=movieid
           JOIN actor   ON actorid=actor.id
where ord = 1 and movieid in 
(select movie.id from
 movie JOIN casting ON movie.id=movieid
       JOIN actor   ON actorid=actor.id 
and name = 'Julie Andrews')

14. 列出按字母順序,列出哪一演員曾作30多次第1主角

select name
from casting JOIN actor ON actorid=actor.id
where ord = 1
group by name
having count(movieid)>=30
order by name

15. 列出1978年首影的電影名稱及角色數目,按此數目由多至少排列。

select title,count(actorid) as a
from movie JOIN casting ON movie.id=movieid
where yr=1978
group by title
order by a desc

16. 列出曾與演員亞特·葛芬柯'Art Garfunkel'合作過的演員姓名。

select name
from casting JOIN actor ON actorid=actor.id
where name !='Art Garfunkel'
and movieid in (select movieid 
from casting JOIN actor ON actorid=actor.id
where name = 'Art Garfunkel')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值