我有两张桌子:
Detection{
DetectionId int
....
...
...
}
DetectionInfo{
DetectionId int
Name char
val char
}
这两个表之间存在一对多的关系。
我想检索BOTH val =“DOWN”和val =“Blue”
的检测结果这是我的解决方案:
SELECT * FROM Detection a WHERE a.DetectionId IN
(SELECT c.DetectionId FROM DetectionInfo c where c.val = "DOWN" and c.Detection IN
(SELECT d.DetectionId FROM DetectionInfo d
WHERE d.val = "Blue"))
有更好(有效)的方法吗?
答案 0 :(得分:1)
以下是获取ID的一种方法:
select detectionid
from detectioninfo
where val in ('DOWN', 'BLUE')
group by detectionid
having count(distinct val) = 2;
如果需要,您可以使用join
,in
或exists
从detection
表中获取其他信息。