这是一项硬件任务,所以如果你可以提供帮助,请不要给出确切的答案;我想学习,不是为我做的。
(创建表的详细信息在本文末尾)
我的任务是找到所有与汤姆汉克斯合影的演员,按电影名称排序,使用2个查询。
到目前为止,我已经能够创建以下查询;我知道我的加入是错的,但我不确定为什么。我怎么能这样想?我觉得我接近答案,但不是那里。
SELECT actor.name, movie.title FROM actor
LEFT OUTER JOIN character.movie_id ON movie.id IN
(
-- Get the ID of every movie Tom Hanks was in
SELECT movie_id FROM actor
INNER JOIN character ON character.actor_id = actor.id
WHERE actor.name = 'Tom Hanks'
)
WHERE actor.name != 'Tom Hanks'
ORDER BY movie.title;
以下是架构的创建表状态:
create table actor (
id varchar(100),name varchar(100),
constraint pk_actor_id primary key (id));
create table movie(
id varchar(100),
title varchar(100),
year smallint unsigned,
mpaa_rating varchar(10),
audience_score smallint unsigned,
critics_score smallint unsigned,
constraint pk_id primary key(id));
create table character(
actor_id varchar(100),
movie_id varchar(100),
character varchar(100),
constraint pk_character_id primary key(movie_id, actor_id, character),
constraint fk_actor_id foreign key (actor_id) references actor (id),
constraint fk_movie_id foreign key (movie_id) references movie (id));
答案 0 :(得分:3)
左外连接将为您提供左表(actor)中的每个条目。如果它在右表中有相应的值,它将为您提供该值,否则,您将获得返回的空值。
此外,您加入了一个表格。在您的查询中,您正在尝试加入列
答案 1 :(得分:1)
也许这样的事情? FWTH是汤姆汉克斯的电影。
select mo.title, ac.name
from character ch join
(select m.movie_id
from character c join movie m on c.movie_id = m.id
join actor a on a.id = c.actor_id
where a.name = 'Tom Hanks'
) fwth on ch.movie_id = fwth.movie_id
join actor ac on ac.id = ch.actor_id
join movie mo on mo.id = fwth.movie_id
order by mo.title;