我有三张桌子
Person
- id
- other_fields
PersonTypes
- id
- person_id
- type_id
PersonEmails
- id
- person_id
- email_address
如何只获取PersonTypes.type_id与value
不同的电子邮件地址?
我使用MariaDB v.10,我尝试在所有表中使用左外连接,然后只返回type_id不同的这些行。但我不确定结果。
SELECT pe.email_address
FROM Person p
LEFT OUTER JOIN PersonTypes pt ON p.id = pt.person_id
LEFT OUTER JOIN PersonEmails pe ON p.id = pe.person_id
WHERE pt.type_id != 14;
答案 0 :(得分:1)
select P1.id,
P2.type_id, -- just for the sake of this output
P2.email_address
from Person P1
inner join
(
select PT.person_id, PT.type_id, PE.email_address
from PersonTypes PT
inner join PersonEmails PE
on PE.person_id = PT.person_id
where PT.type_id <> 14
) P2
on P2.person_id = P1.id
或
select pe.email_address
from PersonEmails pe
where person_id not in
(
select person_id
from PersonTypes
where Type_id = 14
)
答案 1 :(得分:0)
查询选择所有没有type_id 14并加入Person和PersonsEmails表数据的人
SELECT pe.email_address
FROM PersonTypes pt
INNER JOIN Person p ON pt.person_id = p.id
INNER JOIN PersonEmails pe ON pt.person_id = pe.person_id
WHERE pt.type_id != 14;