0

这是我的代码,稍后我会将其放入 php 中,但目前我在提供的查询测试器中使用它。我们正在做一级凯文培根而不是六级。

我的查询如下:

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 

如何让它只显示神秘河?

4

4 回答 4

1

如果我正确理解了您的问题,则您正在尝试查找其中包含的电影Sean Penn列表Kevin Bacon。如果是这样,那么您将需要在查询中使用两次演员和角色表。

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;
于 2012-11-07T05:48:14.200 回答
1

一、分组条件

WHERE (actors.first_name= 'kevin' AND actors.last_name='bacon') OR 
      (actors.first_name= 'sean' AND actors.last_name='penn')

二、DISTINCTSELECT关键字后面加

SELECT DISTINCT ...

后续问题:你为什么只想展示Mystic River

于 2012-11-07T05:32:33.390 回答
1

我认为你在这里要求的是不同的......

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;

和 THIS 在 where 子句中,

 WHERE (actors.first_name= 'kevin' AND actors.last_name='bacon') AND (actors.first_name= 'sean' AND actors.last_name='penn')

如果你想知道凯文培根和肖恩潘在同一部电影中的位置

于 2012-11-07T05:34:38.083 回答
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;
于 2012-11-07T05:31:51.050 回答