MySQL Workbench - 如何从多个where子句获得结果?

时间:2014-07-22 18:56:47

标签: mysql mysql-workbench

我试图提取与一个或多个保管人有关系的机构名称。

例如,让我们说机构A与保管人1,2和3有关系然后我希望结果只显示机构A,而不是机构B和C.基本上我只想看到拥有托管人1,2,3的机构,而不是仅与托管人1或2或3有关系的机构。

(希望这个问题有点意义)

以下是我正在处理的SQL:

select i.institution_name, custodian_name
from institution i
join institution_has_custodian ihcu
on i.institution_id = ihcu.institution_id
join custodian c
on c.custodian_id = ihcu.custodian_id
where c.custodian_name in ('JPMorgan (00902/82D)','BNY Mellon (Formerly Bony) (00901/93I)')

1 个答案:

答案 0 :(得分:1)

您可以使用外部查询并按institution_name分组,如下所示

select institution_name, count(custodian_name) as cust_count
from
(
select i.institution_name, custodian_name
from institution i
join institution_has_custodian ihcu
on i.institution_id = ihcu.institution_id
join custodian c
on c.custodian_id = ihcu.custodian_id
where c.custodian_name in ('JPMorgan (00902/82D)',
'BNY Mellon (Formerly Bony) (00901/93I)')
) tab
group by institution_name
having cust_count = 3