1
ACTOR (id, fname, lname, gender)
MOVIE (id, name, year, rank)
CASTS (pid, mid, role)
WHERE pid references ACTOR id
mid references Movie id

x が y なしで出演した映画を挙げてください (x と y は俳優です)。

NOT を使用して SQL を作成するのは難しいと感じています。これは私の試みです。2番目の俳優がいないため、仕上げることができません

SELECT m.name
FROM MOVIE m
WHERE m.id NOT IN (SELECT c.mid
                       FROM CASTS c, ACTOR a
                       WHERE c.pid = a.id AND a.name = "adam..") 
4

4 に答える 4

4

使用NOT EXISTS:

SELECT m.name                       -- Show the names                      
FROM movie m                        -- of all movies
WHERE EXISTS                        -- that there was
      ( SELECT *                    -- a role
        FROM casts c                -- casted to
          JOIN actor a              -- actor with
            ON c.pid = a.id
        WHERE c.mid = m.id  
          AND a.name = 'Actor X'    -- name X
      ) 
  AND NOT EXISTS                    -- and there was not
      ( SELECT *                    -- any role
        FROM casts c                -- casted
          JOIN actor a              -- to actor with
            ON c.pid = a.id
        WHERE c.mid = m.id 
          AND a.name = 'Actor Y'    -- name Y
      ) ;

も使用できますNOT IN。または列NULLに次のような行がある場合、予期しない結果が生じる可能性があることに注意してください。movie.idcasts.mid

SELECT m.name                       -- Show the names                      
FROM movie m                        -- of all movies
WHERE m.id IN                       -- but keep only the movies that
      ( SELECT c.mid                -- movies that
        FROM casts c                -- had a role casted to
          JOIN actor a              -- actor with
            ON c.pid = a.id
        WHERE a.name = 'Actor X'    -- name X
      ) 
  AND m.id NOT IN                   -- and not the movies
      ( SELECT c.mid                -- that
        FROM casts c                -- had a role casted
          JOIN actor a              -- to actor with
            ON c.pid = a.id
        WHERE a.name = 'Actor Y'    -- name Y
      ) ;
于 2013-05-02T16:38:20.617 に答える
1
SELECT  a.*
FROM    Movie a
        INNER JOIN Casts b
            ON a.ID = b.mID
        INNER JOIN Actor c
            ON b.pid = c.ID
        LEFT JOIN
        (
            SELECT  aa.mid
            FROM    Casts aa
                    INNER JOIN Actor bb
                        ON aa.pid = bb.ID
            WHERE   bb.fName = 'Y_Name'
        ) d ON  a.id = d.mid
WHERE   c.fname = 'X_Name' AND
        d.mid IS NULL

サブクエリで追加の結合を行う理由は、アクターの名前でレコードをフィルタリングしているためです。


これらの一連のレコードがあると仮定します

俳優

╔════╦════════╗
║ ID ║ FNAME  ║
╠════╬════════╣
║  1 ║ X_Name ║
║  2 ║ Y_Name ║
╚════╩════════╝

映画

╔════╦══════╗
║ ID ║ NAME ║
╠════╬══════╣
║  1 ║ Mov1 ║
║  2 ║ Mov2 ║
║  3 ║ Mov3 ║
╚════╩══════╝

キャスト

╔═════╦═════╗
║ PID ║ MID ║
╠═════╬═════╣
║   1 ║   1 ║ <<== EXPECTED OUTPUT since Y_NAME is not present
║   1 ║   2 ║                      on Movie Mov1
║   2 ║   2 ║
║   1 ║   3 ║
║   2 ║   3 ║
╚═════╩═════╝

出力

╔════╦══════╗
║ ID ║ NAME ║
╠════╬══════╣
║  1 ║ Mov1 ║
╚════╩══════╝
于 2013-05-02T16:15:00.557 に答える
1

最初に俳優 X のすべてのムービーを取得します。次に、俳優 Y も含むムービーをフィルターで除外します。

SELECT m.name
FROM MOVIE m, CASTS c, ACTOR a
WHERE m.id = c.mid
    AND c.pid = a.id
    AND a.name = "ACTOR X"
    AND NOT EXISTS (
        SELECT 1
        FROM CASTS c1, ACTOR a1
        WHERE c1.pid = a1.id
            AND m.id = c1.mid
            AND a1.name = "ACTOR Y"
    )
于 2013-05-02T16:17:40.260 に答える