我如何执行这个简单的 SQL SELECT 语句?

发布于 2024-10-07 00:40:45 字数 441 浏览 5 评论 0原文

我在使用我认为应该是最简单的 SQL 语句时遇到了麻烦,但是它对我不起作用...

我有 4 个表:S、P、J 和 SPJ,其中 SPJ 基本上是将所有 ID 连接在一起的表。

现在,所需的语句是:“获取供应 P2 和 P4 零件的供应商的供应商编号。”

从 SPJ 选择 SNO,其中 PNO = 'P2' AND PNO = 'P4'
- 不返回任何结果(这是错误的)

SELECT SNO FROM SPJ WHERE PNO = 'P2' UNION SELECT SNO FROM SPJ WHERE PNO = 'P4'
- 返回结果,加上一个仅提供两者之一的额外内容...

我尝试了各种语句但无法弄清楚,我知道它必须很简单,但我就是看不到它...

有人有什么想法吗?

干杯

I'm having trouble with what I think should be the simplest of SQL statements, however it is not working for me...

I have 4 tables: S, P, J and SPJ where SPJ is basically a table linking all the ID's together.

Now, the statement required is: "Get supplier numbers for suppliers who supply part P2 and part P4."

SELECT SNO FROM SPJ WHERE PNO = 'P2' AND PNO = 'P4'
- returns no results (which is wrong)

SELECT SNO FROM SPJ WHERE PNO = 'P2' UNION SELECT SNO FROM SPJ WHERE PNO = 'P4'
- returns the result, plus an extra which only supplies one of the two...

I've tried all manner of statements but can't figure it out, I know it's got to be simple, but I just can't see it...

Anybody got any ideas?

Cheers

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

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

发布评论

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

评论(5

初见 2024-10-14 00:40:46
SELECT SNO FROM SPJ WHERE PNO in ('P2','P4')
SELECT SNO FROM SPJ WHERE PNO in ('P2','P4')
小兔几 2024-10-14 00:40:46

最好使用 OR

SELECT SNO FROM SPJ WHERE PNO = 'P2' OR PNO= 'P4'

Better to use OR

SELECT SNO FROM SPJ WHERE PNO = 'P2' OR PNO= 'P4'
莫相离 2024-10-14 00:40:46

“SELECT SNO FROM SPJ WHERE PNO = 'P2' AND PNO = 'P4'”

显然 PNO 不能同时是 P2 和 p4。
在不了解数据库方案的情况下很难回答你的问题,你应该给出你的表的描述。

"SELECT SNO FROM SPJ WHERE PNO = 'P2' AND PNO = 'P4'"

obviously PNO can't be both P2 and p4 at the same time.
it's difficult to answer your question without a knowing the db scheme, you should give a description of your tables.

冷心人i 2024-10-14 00:40:45

这将为您提供提供两种零件的供应商,而不是提供其中一个/或一个的供应商。

select sno
    from spj
    where pno in ('P2','P4')
    group by sno
    having count(distinct pno) = 2

This will get you suppliers who provide both parts, as opposed to those who supply either/or.

select sno
    from spj
    where pno in ('P2','P4')
    group by sno
    having count(distinct pno) = 2
傻比既视感 2024-10-14 00:40:45

或者作为@Sathya 的替代品

SELECT SNO 
FROM SPJ 
WHERE PNO = 'P2'
OR PNO = 'P4'

Or as an alternative to @Sathya

SELECT SNO 
FROM SPJ 
WHERE PNO = 'P2'
OR PNO = 'P4'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文