我试图拉扯与一个或多个保管人有关系的机构名称。
例如,假设机构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)')
最佳答案
您可以使用外部查询并按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
关于mysql - MySQL Workbench-如何从多个where子句获取结果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24895503/