SQL语言查询

时间:2015-03-01 12:54:33

标签: sql

首先,给出这个表

CREATE TABLE play(
movie VARCHAR(256), 
scene VARCHAR(256),
actor VARCHAR(256));

我想找一些扮演安吉丽娜·朱莉正在演奏的电影演员的名字。这会有用吗

SELECT p.actor
FROM play p
WHERE 
p.actor NOT LIKE 'Angelina Jolie'
AND p.movie IN (SELECT p.movie
                FROM play p
                WHERE p.actor = 'Angelina Jolie');

第二个问题:我想打印只播放安吉丽娜·朱莉正在播放的电影的演员的名字。这会有用吗

SELECT p.actor
FROM play p
WHERE 
p.actor NOT LIKE 'Angelina Jolie'
AND p.movie = ALL(SELECT p.movie
                  FROM play p
                  WHERE p.actor = 'Angelina Jolie');

1 个答案:

答案 0 :(得分:0)

您的第一个查询将起作用,而不是第二个。

这样可以起作用:

     SELECT distinct actor
    FROM play p
    WHERE p.actor <> 'Angelina Jolie'
    and movie in (SELECT p.movie
                      FROM play p
                      WHERE p.actor = 'Angelina Jolie')
     and actor not in 
    -- actors in movies without Jolie
    (SELECT p.actor
    FROM play p
    WHERE  movie in
    (select distinct movie
    from play
    where movie not in(
    select p.movie
    from play p
    WHERE p.actor = 'Angelina Jolie')
    ))