sqlzoo - 7 More JOIN operations

本文详细介绍了SQLZOO中的7个JOIN操作实例,包括获取电影《Casablanca》的演员列表、Harrison Ford的电影、非主演角色、1962年电影的主演、Rock Hudson忙碌的年份、Julie Andrews电影的主演以及与Art Garfunkel合作的演员。通过不同的SQL查询方法,如子查询、内部联结和多表联结,展示了如何在实际问题中使用JOIN操作。
摘要由CSDN通过智能技术生成

- 代表省略 与前面题型相同
* 代表错题,重点看
? 代表有疑问


6. ? Cast list for Casablanca

Obtain the cast list for ‘Casablanca’.
The cast list is the names of the actors who were in the movie.
Use movieid=11768, (or whatever value you got from the previous question)

获取Casablanca电影中所有的演员名

通过Casablanca电影名获取唯一的电影ID, 通过电影ID获取多个参演的演员ID, 通过每个演员ID获取对应的演员名 (电影名和电影ID一一对应, 演员名和演员ID一一对应)

  • 方法1: 使用内部联结 + 子查询
select name from actor 
join casting on actor.id = actorid 
where movieid in (select id from movie where title = 'Casablanca')

join on 内部联结后接where子句, where后movieid in / moveid =都可以

  • 方法2:使用子查询
select name from actor where id in 
(select actorid from casting where movieid = 
(select id from movie where title = 'Casablanca' )) 

select 嵌套 这里select id from movie where title = 'Casablanca'返回唯一值11768, 前面使用movieid = 答案正确, 使用movieid in 答案错误? 但使用 movieid in ('11768') 答案正确, 不知道原因???

  • 方法3: 联结多个表
select name from actor join casting on actor.id = actorid 
join movie on movieid = movie.id and title = 'Casablanca'

join on 联结可以多次使用, and title = 'Casablanca' 条件可以用 and 连接, 也可以用where子句

select name from actor, casting, movie 
where actor.id = actorid and movieid = movie.id 
and title = 'Casablanca'

与上面方式等效

7. - Alien cast list

与6同, 几种方式都可以实现

8. - Harrison Ford movies

与6同, 几种方式都可以实现

9. Harrison Ford as a supporting actor

List the films where ‘Harrison Ford’ has appeared - but not in the starring role. [Note: the ord field of casting gives the position of the actor. If ord=1 then this actor is in the starring role]


列出 ‘Harrison Ford’ 参演但不是主演的电影

由于有两个条件在不同的表中,采用联结多个表的方式筛选条件
1、演员名为 ‘Harrison Ford’ (在表actor 中)
2、不是主演(ord<>1 在表 casting 中)

-- 方法1 join on 
select title from movie 
join casting on movie.id = movieid 
join actor 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值