0

所以我坚持尝试制作一个 SQL 来抓取对特定 COLUMN 具有相同值的每一行。

示例:
表格:买家

aID | bID                        aID | bID
 1  |  3                          1  |  3 
 1  |  4                          1  |  2
 1  |  2                          2  |  2
 1  |  5                          1  |  4    this one also should return aID = 1
                                       since aID =1 is present for every different bID

我想我很接近:

SELECT aID
  FROM buyers
 WHERE COUNT(bID) = (SELECT S1.aID
                       FROM buyers S1, 
                            buyers S2
                      WHERE S1.aID = S2.bID
                        AND S2.bID <> S1.bID)

它必须在一列中的每一行中,否则不要得到它,所以如果一个aID不同,那就不要得到它。anaID必须与每个匹配bID

4

2 回答 2

3
SELECT aID
FROM buyers
GROUP BY aID
HAVING COUNT(*) = (SELECT COUNT(distinct bID) FROM buyers)

小提琴

这假设每个aID,bID组合都是唯一的。如果bID可以重复相同的aID,你需要这样做:

SELECT aID
FROM (SELECT DISTINCT aID, bID FROM buyers)
GROUP BY aID
HAVING COUNT(*) = (SELECT COUNT(distinct bID) FROM buyers)
于 2013-04-11T01:30:02.087 回答
0

怎么样

Select Distinct aId 
From Table t
Where Not Exists
   (Select * From table i
    Where bId <> t.bId
       And Not Exists 
           (Select * From table
            Where bId = i.bId
              And aId <> t.aId))

这说:

向我展示所有不同的值,aId使得具有不同值的行都不aId包含一个bId值,以至于甚至没有一行具有bId相同的值aId

于 2013-04-11T02:11:02.483 回答