0

我有一个只包含两列的表:

ObjectID||PropertyID 

任务:通过 PropertyID == (P1 and P2 and P3 and ...) 获取所有 ObjectID。

我解决了这个任务:

  SELECT *
  FROM  ( 
        SELECT SD.ObjectID ObjectID,
               count( SD.ObjectID )  countMatchingProperties
          FROM table AS SD
         WHERE SD.PropertyID IN ( P1, P2, P3, ..., Pn ) 
         GROUP BY ObjectID 
    ) 
    AS C
 WHERE C.countMatchingProperties > n-1

但我并没有想到,这项任务可以更轻松、更快速地解决。

4

2 回答 2

1
SELECT SD.ObjectID ObjectID,
count( SD.ObjectID )  countMatchingProperties
FROM table AS SD
WHERE SD.PropertyID IN ( P1, P2, P3, ..., Pn ) 
GROUP BY ObjectID 
having count( SD.ObjectID ) > n-1
于 2012-07-10T14:01:54.723 回答
0
select ObjectID 
from table 
where PropertyID in (P1, P2, P3, P4)
group by ObjectID
having count(distinct PropertyID) = 4

计数(例如, )必须与您的子句4中唯一值的数量相匹配。IN

于 2012-07-10T14:03:58.180 回答