还是通过下面的3
张表进行练习
找出毛利小于预算的导演 需要注意的是导演们的名字也是在actor表中的
select name
from actor
inner join movie on actor.id=director -- actor表中的id和director相同即可
where gross < budget;
3个表的联结通过两个Join实现:
actor—>casting—>movie
select *
from actor
join casting on actor.id=actorid
join movie on movie.id=movieid;
找出由名字中包含
John
的演员的电影(通配符的使用),根据电影数量排序 笔记:order by
排序的时候可以使用字段名字,也可以使用字段的相对位置
select name, count(movieid) -- 统计数量
from casting
join actor on actorid=actor.id -- 两个表的联结
where name like 'John %'
group by name -- 名字分组
order by 2 desc -- 2 表示的是第2个字段count(movieid)
选择由该演员出演主角的电影
select title
from movie
join casting on (movieid=movie.id)
join actor on (actorid=actor.id)
where name='Paul Hogen'
and ord=1
找出由RS导演的电影的主演
select name
from movie
join casting on movie.id=movieid
join actor on actor.id=actorid
where ord=1 -- 主演
and director = 351 -- 导演编号
两种方式将
movie
和actor
表进行联结
通过代码选出正确答案
select title, yr
from movie, casting, actor
where name='Robert De Niro'
and movieid=movie.id
and actorid=actor.id
and ord=3 -- 排名第3