我已经为此苦苦挣扎了一段时间,所以也许有人可以得出一些见解。
我们有一个来自TVShow数据库的实践查询,该数据库有4个表。
这是查询:由ABC赞助所有电视节目的赞助商
到目前为止,我已经尝试过了,但是似乎没有用:SELECT DISTINCT RSPONSOR.SPONSOR_NAMEFROM RSPONSORWHERE 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
最佳答案
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'
);
关于mysql - 文本的SQL查询问题(嵌套不存在),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27512713/