Select parent if all children meet criteria

為{幸葍}努か 提交于 2019-12-03 12:31:27

You can use NOT EXISTS

SELECT id 
FROM Parent p
WHERE NOT EXISTS
(
   SELECT 1 FROM Child c
   WHERE c.parent_Id = p.id
   AND c.x <> c.y
)

Edit: Here's the sql-fiddle: http://sqlfiddle.com/#!3/20128/1/0

This is what you need?

  select id from parent where id not in(
    select parent_id from chile 
    where x<>y
    group by parent_id)

Should join 2 tables first because the parents does not have children that will satisfy

And should add index for pa_id column

SELECT DISTINCT pa.id 
FROM pa INNER JOIN c ON c.pa_id = pa.id 
WHERE NOT EXISTS ( SELECT 1 FROM c WHERE c.parent_Id = p.id and c.x <> c.y )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!