PROPERTY_FOR_RENT (Pno, Street, Area, City, Pcode, Type, Rooms, Rent, Ono, Sno, Bno)
OWNER (Ono, Fname, Lname, Address, Tel_No)
查找仅在租赁代理机构注册的房屋的业主的名称和地址。
有两种类型的房产(房屋,平房)。 “只有”我对它没有任何理想
SELECT Fname, Lname, Address
FROM owner
WHERE Ono
IN (
SELECT Ono
FROM property_for_rent
WHERE TYPE = "house"
AND Ono NOT
IN (
SELECT Ono
FROM property_for_rent
WHERE TYPE <> "house"
)
)
这就是我试过的方式。还有更好的办法吗?
答案 0 :(得分:1)
您可以通过多种方式完成此操作。一种方法是使用group by
和having
。另一种方法是使用not exists
:
select o.*
from owner o
where not exists (select 1 from property_for_rent pfr where o.ono = pfr.ono and type <> 'house');
答案 1 :(得分:0)
使用以下查询
Select Fname, Lname, Address from Owner
Where Ono IN (Select Ono from PROPERTY_FOR_RENT where Type = 'houses' AND Type != 'flat')
答案 2 :(得分:0)
我相信戈登的解决方案是最优雅的:但是自从我在评论中提到它:这是一种在连接和拥有它的方法。
Select O.Ono, O.Fname, O.Lname, Count(Distinct P1.Type) cp1 , count(Distinct P2.Type) cp2
FROM Owner O
INNER JOIN Property_for_Rent P1
on O.Ono = P1.Ono
LEFT JOIN Property_for_Rent P2
on O.Ono = P1.Ono
and P1.PNo = P2.PNo
and P2.Type = 'Houses'
Group by Ono,FName,LName
having cp1=Cp2
答案 3 :(得分:-1)
第一步: 一起加入表格 提示: 两个表共有哪些列?