我有 3 张桌子:
organiz: pk = org_id
individu: pk = ind_id
address: pk = address_id
两者都organiz
包含individu
一个address_id
. 中的每一行都individu
包含一个org_id
. 基本上,“个人”属于“组织”。我想要一个查询,让我知道address_id
每个人都有特定的org_id
.
这是我以前尝试过的,但它只给了我address_id
所有个人的。
select
a.Address_id,
a.Address_Line_1,
a.Address_Line_2,
a.Address_City,
a.Address_State_Code,
a.Address_Zip,
o.Phone_Number
from
address a inner join individu i on a.address_id = i.address_id
inner join organiz o on o.org_id = i.org_id
where
a.Address_Line_1 is not null
and o.org_id = 808474
为了让它工作,我使用了一个联合来查询组织的地址 ID:
select
a.Address_id,
a.Address_Line_1,
a.Address_Line_2,
a.Address_City,
a.Address_State_Code,
a.Address_Zip,
o.Phone_Number
from
address a inner join individu i on a.address_id = i.address_id
inner join organiz o on o.org_id = i.org_id
where
a.Address_Line_1 is not null
and o.org_id = 808474
UNION
select
a.Address_id,
a.Address_Line_1,
a.Address_Line_2,
a.Address_City,
a.Address_State_Code,
a.Address_Zip,
o.Phone_Number
from
address a inner join organiz o on a.address_id = o.address_id
where
o.org_id = 808474
and a.Address_Line_1 is not null
是否有另一种方法可以避免使用 UNION?