简体   繁体   English

SQL - 演员之间的关系

[英]SQL - Relationship between actors

I have a single table with 2 columns: Actors and movies which looks like this: 我有一个包含2列的单个表:演员和电影,如下所示:

ACTOR  | MOVIE
-------+-------------
Volta  | Pulp Fiction
Bruce  | Pulp Fiction
Rhame  | Pulp Fiction
Walke  | Pulp Fiction
Rhame  | Bad Movie
Bruce  | Bad Movie
Volta  | Decent Movie
Brian  | Decent Movie
Walke  | Awesome Movie
Brian  | Awesome Movie

I want to know know which actors, who appeared in Pulp Fiction, never has appeared in another movie with another actor from Pulp Fiction. 我想知道哪些演员出现在低俗小说中,从未出现在与另一位来自制浆小说的演员的另一部电影中。

From this example, the output should be: 从这个例子中,输出应该是:

Volta
Walke

Because they appeared in Pulp Fiction and in Decent Movie and Awesome Movie respectively without any other actors from Pulp Fiction. 因为他们分别出现在低俗小说和体面电影和令人敬畏的电影中,没有任何其他演员来自低俗小说。

I'm using MySQL. 我正在使用MySQL。

SELECT m.actor
FROM movies m 
WHERE 
m.movie = 'Pulp Fiction'
AND NOT EXISTS
(
  SELECT 1
  FROM movies m1 
    JOIN movies m2 ON m1.movie = m2.movie 
      AND m1.actor <> m2.actor 
      AND m2.movie <> 'Pulp Fiction' 
      AND m2.actor IN (SELECT actor FROM movies WHERE movie = 'Pulp Fiction')
  WHERE
      m.actor = m1.actor 
)

According to SQLFiddle done by ChrisProsser it should give the proper result. 根据ChrisProsser所做的SQLFiddle,它应该给出正确的结果。

There may be an easier way, but this should work: 可能有一种更简单的方法,但这应该有效:

select m.actor 
  from movies m
 where m.movie = 'Pulp Fiction'
   and m.actor not in (
       select m2.actor
         from movies m1,
              movies m2,
              movies m3
        where m1.actor = m2.actor
          and m1.movie = 'Pulp Fiction'
          and m2.movie != m1.movie
          and m3.movie = m2.movie
          and m3.actor in (select m4.actor
                             from movies m4
                            where m4.movie = 'Pulp Fiction')
        group by m2.actor
        having count(*) > 1);

I have created a SQL Fiddle to test this and the output is correct. 我创建了一个SQL Fiddle来测试它,输出是正确的。

Try This.... 尝试这个....

SELECT actor, movie 
FROM actor
where movie like 'Pulp Fiction' AND
movie NOT IN(select movie from tablename t where t.actor like actor and movie NOT LIKE 'Pulp Fiction');
SELECT actor FROM table AS t1  
WHERE movie = 'Pulp Fiction' AND  
NOT EXISTS( 
  SELECT 1 FROM table AS t2 WHERE t2.actor = t1.actor AND movie <> 'Pulp Fiction' 
) 

I recommend you to store the movies and actors in different tables (a 'movie' table and a 'actor' table) though and use a 'movie_actor' table to connect the data to each other. 我建议您将电影和演员存储在不同的表格中(“电影”表和“演员表”),然后使用“movie_actor”表将数据相互连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM