SQL查询文本问题(嵌套NOT EXISTS)

时间:2014-12-16 19:49:08

标签: mysql sql oracle exists

我一直在努力解决这个问题,所以也许有人可以提供一些见解。

我们有一个来自TVShow数据库的练习查询,该问题有4个表。

这是查询:赞助ABC所有电视节目的赞助商

到目前为止,我所尝试的是这个,但它似乎没有起作用:

SELECT DISTINCT RSPONSOR.SPONSOR_NAME FROM RSPONSOR WHERE NOT EXISTS ( SELECT * FROM RTVSHOW WHERE NOT EXISTS ( SELECT * FROM RSPONSORBY WHERE RSPONSOR.SPONSOR_NAME = RSPONSORBY.SPONSOR_NAME AND RSPONSORBY.SHOW_NUM = RTVSHOW.SHOW_NUM AND RTVSHOW.NETWORK_ID = 'ABC' ) );

会喜欢任何帮助!提前谢谢。

以下是供参考的表格

- RTVSHOW -

SHOW_NUM    NUMBER

SHOW_NAME   VARCHAR2(20 BYTE)

START_MONTH NUMBER

START_YEAR  NUMBER

END_MONTH   NUMBER

END_YEAR    NUMBER

NETWORK_ID  VARCHAR2(20 BYTE)

DISTR_NAME  VARCHAR2(20 BYTE)

- RSPONSOR -

SPONSOR_NAME

PARENT_NAME

- RSPONSORBY -

SHOW_NUM

SPONSOR_NAME

- RNETWORK -

NETWORK_ID

NETWORK_HQ

PARENT_NAME

1 个答案:

答案 0 :(得分:0)

select rSponsor.sponsor_name, rSponsor.parent_name
from rSponsor
join rSponsorBy
    on rSponsor.sponsor_name = rSponsorBy.sponsor_name
join rTVShow
    on rSponsorBy.show_num = rTVShow.show_num
join rNetwork
    on rTVShow.network_id = rNetwork.network_id
where rNetwork.network_id = 'ABC'
group by rSponsor.sponsor_name, rSponsor.parent_name
having count(distinct rTVShow.show_num) = --ABC shows sponsored by this sponsor
(
    select count(distinct rTVShow.show_num) --ABC shows
    from rTVShow
    join rNetwork
        on rTVShow.network_id = rNetwork.network_id
    where
        rNetwork.network_id = 'ABC'
);