我得到了具有字段標識,用戶標識,組標識的表「成員」 我想僅選擇每個成員的成員計數大於1的組標識,如下所示:選擇同一表的其他查詢的計數
ID UserID GroupID
1 1 1 /* we are selecting groups for guy with ID 1, here are 3 people */
2 2 1
3 3 1
4 1 2 /* here the guy is alone in group, ignore him! */
5 1 3 /* select this one too for guy 1, because #members>1 */
6 2 3
執行組ID 1包括必須有至少2個的人在同一組的人選擇,所以這將返回:1,3
但我不知道如何在SQL這樣做,我想這樣做,但是,它顯然不起作用:
SELECT GroupID
FROM Members
WHERE UserID=1
AND COUNT(
SELECT UserID FROM Members WHERE GroupID=**currently iterated group**
)>1;
是否有某種可能?