我如何执行这个简单的 SQL SELECT 语句?
我在使用我认为应该是最简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最好使用 OR
Better to use OR
“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.
这将为您提供提供两种零件的供应商,而不是提供其中一个/或一个的供应商。
This will get you suppliers who provide both parts, as opposed to those who supply either/or.
或者作为@Sathya 的替代品
Or as an alternative to @Sathya