0

当 OwnerID 在 Property 中出现多次时,我试图从 Owner 获取 OwnerID、FName、LName。这是我尝试过的,但我对 sql 很陌生,所以我不知道这是否接近。

Select OwnerID, FName, LName 
From Owner 
Where OwnerID = (Select OwnerID 
                 From Property 
                 GROUP BY OwnerID 
                 HAVING Count(*) >=2)
4

3 回答 3

2

你的意思:

Select OwnerID, FName, LName 
From Owner 
Where OwnerID In (Select OwnerID 
                 From Property 
                 Group By OwnerID 
                 Having Count(OwnerId) >= 2)

但是,如果您试图让所有拥有超过 1 个财产的所有者,则假定财产始终只有一个所有者,而现实世界中并非总是如此。

于 2013-02-25T01:55:39.683 回答
2

您可以使用 IN 运算符,如 @Mitch Wheat 答案,或使用 EXISTS :

Select OwnerID, FName, LName 
From Owner a
Where EXISTS (Select b.OwnerID 
                 From Property b
                 WHERE a.OwnerID = b.OwnerID
                 Group By b.OwnerID 
                 Having Count(b.OwnerId) >= 2)
于 2013-02-25T01:59:28.757 回答
1

按表现我会做

Select OwnerID, FName, LName 
From Owner a
Where EXISTS (Select *
             From Property b
             WHERE a.OwnerID = b.OwnerID
             Group By b.OwnerID 
             Having Count(*) >= 2)
于 2013-02-25T04:53:45.060 回答