我的表只包含两列:
ObjectID||PropertyID
任务:获取所有ObjectID和PropertyID ==(P1和P2以及P3和......)。
我解决了这个任务:
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
但我并没有想到这个任务可以更容易,更快地解决。
答案 0 :(得分: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
答案 1 :(得分:0)
select ObjectID
from table
where PropertyID in (P1, P2, P3, P4)
group by ObjectID
having count(distinct PropertyID) = 4
计数(例如4
)必须与IN
子句中唯一值的数量相匹配。