这是我的代码,稍后我会把它放到php中,但暂时我在提供的Query测试器中使用它。我们正在做第一个凯文培根而不是第六个。
我的查询如下:
SELECT name,year
FROM actors
JOIN roles ON roles.actor_id = actors.id
JOIN movies ON movies.id=roles.movie_id
WHERE actors.first_name= 'kevin' AND actors.last_name='bacon' OR actors.first_name= 'sean' AND actors.last_name='penn'
ORDER BY movies.name;
导致
Animal House 1978
Apollo 13 1995
Few Good Men, A 1992
Footloose 1984
Hollow Man 2000
JFK 1991
Mystic River 2003
Mystic River 2003
Planes, Trains & Automobiles 1987
Stir of Echoes 1999
如何让它只展示神秘河?
答案 0 :(得分:1)
首先,将条件分组
WHERE (actors.first_name= 'kevin' AND actors.last_name='bacon') OR
(actors.first_name= 'sean' AND actors.last_name='penn')
其次,在DISTINCT
关键字
SELECT
SELECT DISTINCT ...
后续问题:为什么您只想展示Mystic River
?
答案 1 :(得分:1)
我认为你在这里要求的是DISTINCT ......
DISTINCT关键字可用于仅返回不同的(不同的)值。
试试这个
SELECT DISTINCT name,year
FROM actors
JOIN roles ON roles.actor_id = actors.id
JOIN movies ON movies.id=roles.movie_id
WHERE (actors.first_name= 'kevin' AND actors.last_name='bacon') OR (actors.first_name= 'sean' AND actors.last_name='penn')
ORDER BY movies.name;
和where where子句,
WHERE (actors.first_name= 'kevin' AND actors.last_name='bacon') AND (actors.first_name= 'sean' AND actors.last_name='penn')
如果你想知道Kevin Bacon和Sean Penn在同一部电影中的位置
答案 2 :(得分:1)
如果我理解您的问题,您正在尝试查找其中包含Sean Penn
和Kevin Bacon
的电影列表。如果是这样,那么您需要在查询中两次使用actors和roles表。
SELECT
movies.name,
movies.year
FROM
actors AS actor2 INNER JOIN roles AS role2 ON actor2.ID = role2.actor_id
INNER JOIN actors AS actor1 INNER JOIN roles AS role1 ON actor1.ID = role1.actor_id
INNER JOIN movies ON role1.movie_id = movies.ID ON role2.movie_id = movies.ID
WHERE
actor1.first_name= 'kevin' AND actor1.last_name='bacon'
AND
actor2.first_name= 'sean' AND actor2.last_name='penn'
ORDER BY movies.name;
答案 3 :(得分:0)
试试这个
SELECT DISTINCT name,year
FROM actors
JOIN roles ON roles.actor_id = actors.id
JOIN movies ON movies.id=roles.movie_id
WHERE actors.first_name= 'kevin' AND actors.last_name='bacon' OR actors.first_name= 'sean' AND actors.last_name='penn'
ORDER BY movies.name;