您可以始终使用“不存在”而不是“不是”?

发布于 2025-01-17 20:47:05 字数 425 浏览 0 评论 0原文

我有以下模式: 书(ISBN,标题,类型) 已发布(ISBN,PublisherNumber)

查询:发布者的出版商号码是什么,这些发布者专门出版了类型的“计算机科学”书籍?

我尝试使用“不在中”构建查询,并且效果很好。

SELECT p.Publishernumber 

FROM published p , book b 

WHERE b.ISBN=p.ISBN AND b.Genre = "CS" AND p.publishernumber not in  (

SELECT p.publishernumber 

 FROM Book b , published p 

 WHERE b.ISBN=p.ISBN AND b.Genre != "CS" );

如何使用“不存在”构建相同的查询?

I have the Following Schema :
Book(ISBN,Title,Genre)
Published(ISBN,Publishernumber)

The Query : What are the publisher numbers of the publishers that exclusively publish books of the genre “Computer Science”?

I tried building the query using "Not in" and it worked fine.

SELECT p.Publishernumber 

FROM published p , book b 

WHERE b.ISBN=p.ISBN AND b.Genre = "CS" AND p.publishernumber not in  (

SELECT p.publishernumber 

 FROM Book b , published p 

 WHERE b.ISBN=p.ISBN AND b.Genre != "CS" );

How can i build the same query using "Not Exists" ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还不是爱你 2025-01-24 20:47:05

要使用NOT EXISTS,您需要使用相关子查询。

SELECT p.Publishernumber 
FROM published p 
JOIN book b ON b.ISBN=p.ISBN
WHERE b.Genre = "CS" AND NOT EXISTS  (
    SELECT 1  
    FROM Book b
    JOIN published p2 ON b.ISBN=p.ISBN
    WHERE b.Genre != "CS" AND p2.publishernumber = p.publishernumber);

To use NOT EXISTS you need to use a correlated subquery.

SELECT p.Publishernumber 
FROM published p 
JOIN book b ON b.ISBN=p.ISBN
WHERE b.Genre = "CS" AND NOT EXISTS  (
    SELECT 1  
    FROM Book b
    JOIN published p2 ON b.ISBN=p.ISBN
    WHERE b.Genre != "CS" AND p2.publishernumber = p.publishernumber);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文