题解30 | #使用子查询的方式找出属于Action分类的所有电影对应的title,description#
使用子查询的方式找出属于Action分类的所有电影对应的title,description
http://www.nowcoder.com/practice/2f2e556d335d469f96b91b212c4c203e
自己的答案:新建一个临时表,然后内连接,需要给临时表命名,如果临时表写做select * ,就会报错重复的film_id
select a.title, a.description from (select f.film_id, f.description, fc.category_id, f.title from film as f inner join film_category as fc on f.film_id = fc.film_id) as a inner join category as c on a.category_id=c.category_id where name='Action'别人答案1:子查询套娃型选手,貌似所有连接都可以这样写,就是看的有点费劲
select f.title,f.description from film as f where f.film_id in (select fc.film_id from film_category as fc where fc.category_id in (select c.category_id from category as c where c.name = 'Action'));别人答案2:和我的答案的区别在于不用给临时表命名,暂时不太理解,先记住就好了
select f.title,f.description from film as f inner join film_category as fc on f.film_id = fc.film_id inner join category as c on c.category_id = fc.category_id where c.name = 'Action';别人答案3:简单粗暴,但是会形成笛卡尔积
根据题意,最简单粗暴的解法是直接 FROM 三张表查询,且用 WHERE 并列三个限定条件:
1、三个限定条件分别是【f.film_id = fc.film_id】、【fc.category_id =
c.category_id 】、【c.name = 'Action'】
c.category_id 】、【c.name = 'Action'】
SELECT f.title, f.description FROM film f, film_category fc, category c WHERE f.film_id = fc.film_id AND fc.category_id = c.category_id AND c.name = 'Action'