SQL 子查询替代 INTERSECT
我有两张桌子: P
和 PC
(由 Id 列连接的主/详细信息)
Table P:
Id integer
Name varchar(12)
Table PC:
Id integer
Code varchar(12)
Val number
我想从 P 中获取满足以下同时条件的所有名称:
拥有一台带有 < code>PC.Code='A' 和
Val>100
有另一台 PC 具有
PC.Code='B'
和Val>80
总之,我只对那些详细信息符合这两个条件的 P.Name 感兴趣。有没有一种方法可以在不使用 INTERSECT 的情况下进行选择?
INTERSECT 查询是:(
Select P.Name
from P, PC
where P.Id=PC.Id
and PC.Code='A' and Val>100
INTERSECT
Select P.Name
from P, PC
where P.Id=PC.Id
and PC.Code='B' and Val>80
兴趣是检查性能并允许查询在 Access 中运行)
I have two tables:P
and PC
(master/detail joined by the column Id)
Table P:
Id integer
Name varchar(12)
Table PC:
Id integer
Code varchar(12)
Val number
I want to get all Names from P that satisfy the following simultaneous conditions:
have a PC with
PC.Code='A'
andVal>100
have another PC with
PC.Code='B'
andVal>80
In summary, I'm only interested in those P.Name where the details comply with both conditions. Is there a way to select without resorting to INTERSECT?
The INTERSECT query is:
Select P.Name
from P, PC
where P.Id=PC.Id
and PC.Code='A' and Val>100
INTERSECT
Select P.Name
from P, PC
where P.Id=PC.Id
and PC.Code='B' and Val>80
(The interest is to check performance and also to allow the query to be run in Access)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不知道性能怎么样..试试吧..
Do not know how the performance is .. try it ..
这是一种在关系上等效的替代方法(即消除重复行):
以下是几个在语义上等效的替代方法(因为它们可能返回重复行):
Here is an alternative approach that is relationally equivalent (i.e. eliminating duplicate rows):
Here are a couple of alternatives that are semantically equivalent (in that they may return duplicate rows):
使用表别名 P1 和 P2 允许您进行 3 路联接。不过,这并不完全是自连接。这次不行。
Using table aliases P1 and P2 allows you to do a 3-way join. It isn't quite a self-join, though; not this time.
实际上不会使用这个,而是替代方案...
或者对于 Microsoft Access,having 子句需要是
Wouldn't actually use this but an alternative...
Or for Microsoft Access the having clause would need to be