我似乎无法弄清楚如何写这个。我有四张桌子:奖项的主要列表,然后奖励实际发给演员,制片人和电视节目。后一组可通过复合键识别。我想列出所有没有给出的奖项。这是我到目前为止所做的。
SELECT AWARD.AWARD_NAME, AWARD.AWARD_TYPE, AWARD.AWARD_YEAR
FROM AWARD
WHERE NOT EXISTS
(
SELECT *
FROM AWARD AW, ACTORAWARD ACAW
WHERE (AW.AWARD_NAME = ACAW.AWARD_NAME AND AW.AWARD_TYPE = ACAW.AWARD_TYPE AND AW.AWARD_YEAR = ACAW.AWARD_YEAR)
)
AND NOT EXISTS
(
SELECT *
FROM AWARD AW, PRODAWARD PRAW
WHERE (AW.AWARD_NAME = PRAW.AWARD_NAME AND AW.AWARD_TYPE = PRAW.AWARD_TYPE AND AW.AWARD_YEAR = PRAW.AWARD_YEAR)
)
AND NOT EXISTS
(
SELECT *
FROM AWARD AW, SHOWAWARD SHAW
WHERE (AW.AWARD_NAME = SHAW.AWARD_NAME AND AW.AWARD_TYPE = SHAW.AWARD_TYPE AND AW.AWARD_YEAR = SHAW.AWARD_YEAR)
)
语法很好,所以我并不担心,但不存在不起作用,我无法使用IN
同时比较三个值,那么我该怎么做呢解决这个问题?
答案 0 :(得分:2)
您不需要在子选择中加入联接。改为使用共同相关的子查询:
SELECT aw.award_name, aw.award_type, aw.award_year
FROM award aw
WHERE NOT EXISTS (SELECT *
FROM actoraward acaw
WHERE aw.award_name = acaw.award_name
AND aw.award_type = acaw.award_type
AND aw.award_year = acaw.award_year)
AND NOT EXISTS (SELECT *
FROM prodaward praw
WHERE aw.award_name = praw.award_name
AND aw.award_type = praw.award_type
AND aw.award_year = praw.award_year)
AND NOT EXISTS (SELECT *
FROM showaward shaw
WHERE aw.award_name = shaw.award_name
AND aw.award_type = shaw.award_type
AND aw.award_year = shaw.award_year);
我无法使用
IN
同时比较三个值
是的,您可以使用IN
子句比较多个值:
SELECT *
FROM award aw
WHERE (aw.award_name, aw.award_type, aw.award_year)
NOT IN (
SELECT acaw.award_name, acaw.award_type, acaw.award_year
FROM actoraward acaw
UNION
SELECT praw.award_name, praw.award_type, praw.award_year
FROM prodaward praw
UNION
SELECT shaw.award_name, shaw.award_type, shaw.award_year
FROM showaward shaw
)
这很可能比EXISTS