我该如何执行此 SQL SELECT 语句?

发布于 2024-10-12 14:28:07 字数 242 浏览 1 评论 0原文

我有一个名为 SPJ 的表,其中包含 SNo、PNo、JNo 和数量。 SQL语句问题是:

获取供应P3零件但不供应P5零件的供应商的供应商编号。

现在我确信这应该很简单,但我无法让它返回正确的结果!

它应该只返回“S3”,因为它们是唯一提供第 3 部分但不提供第 5 部分的供应商,但它始终返回提供这两者的供应商数量,无论 NOT、<> 和 NOT 的组合如何。等被使用。

I have a table called SPJ with SNo, PNo, JNo and Quantity. The SQL statement problem is:

Get the supplier number of suppliers who supply part P3 but do not supply part P5.

Now I'm sure this should be simple, but i cannot get it to return the right results!

It should just return "S3", as they are the only ones who supply part 3 but not 5, yet it always returns the number of suppliers who supply both, no matter what combination of NOT, <> etc is used.

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

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

发布评论

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

评论(3

酒绊 2024-10-19 14:28:07
SELECT  sno
FROM    (
        SELECT  DISTINCT sno
        FROM    spj
        WHERE   pno = 3
        ) q
WHERE   sno NOT IN
        (
        SELECT  sno
        FROM    spj
        WHERE   pno = 5
        )
SELECT  sno
FROM    (
        SELECT  DISTINCT sno
        FROM    spj
        WHERE   pno = 3
        ) q
WHERE   sno NOT IN
        (
        SELECT  sno
        FROM    spj
        WHERE   pno = 5
        )
陌伤ぢ 2024-10-19 14:28:07

查找 EXCEPT 子句。

这是一个两步的事情。您需要编译第 3 部分的列表,并为第 5 部分创建一个单独的列表,然后将其删除。 EXCEPT 子句是实现此目的的一种方法。或者您可以使用“不存在的地方”

Look up the EXCEPT clause.

It's a two step thing. You need to compile the list for Part 3, and have a separate list for Part 5 that is then removed. the EXCEPT clause is one way to do this. Or you can use "Where not exists"

走过海棠暮 2024-10-19 14:28:07
select distinct sno  # distinct in case of duplicates
from spj A
left join spj B on A.sno = B.sno and B.pno = 5  # same supplier supplies part 5
where A.pno = 3  # supplies part 3
  and B.sno is null  # no match on left join
select distinct sno  # distinct in case of duplicates
from spj A
left join spj B on A.sno = B.sno and B.pno = 5  # same supplier supplies part 5
where A.pno = 3  # supplies part 3
  and B.sno is null  # no match on left join
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文