我有一张如下表:
id pid status
10 1 0
11 1 0
10 2 0
15 1 0
11 2 0
10 3 0
我需要基于id
值的常见pid
。这意味着所有id
的常见pids
值。
所以在上表中预期的输出是:
id
10
我曾尝试过以下查询,但它无法按预期运行:
SELECT DISTINCT id
FROM mytable COUNT(1)
FROM mytable
WHERE pid IN (1,2,3)
GROUP BY checkpointid
答案 0 :(得分:4)
试试这个:
SELECT Id
FROM Tbl
GROUP BY Id
HAVING COUNT(DISTINCT Pid) = (
SELECT COUNT(DISTINCT Pid)
FROM Tbl
)