Select 所有 parents 有 child 和特定 属性
Select all parents having a child with specific property
考虑以下伪表:
Parent(id)
Child(id, parent_id, property)
我如何 select 所有 parents 都具有 child 其中 属性 在 PostgreSQL 中具有特定值?
您可以使用 exists
:
select p.*
from parent p
where exists (select 1
from child c
where c.parent_id = p.id and c.property = ?
);
考虑以下伪表:
Parent(id)
Child(id, parent_id, property)
我如何 select 所有 parents 都具有 child 其中 属性 在 PostgreSQL 中具有特定值?
您可以使用 exists
:
select p.*
from parent p
where exists (select 1
from child c
where c.parent_id = p.id and c.property = ?
);