0

これらの 2 つのテーブルとは 1 対多の関係があります。

p2c {
    parent_id,
    child_id
}

child {
    child_id, -- pk
    count1,
    count2
}

私がこれを行う場合:

select distinct parent_id from p2c 
join child on p2c.child_id = child.child_id
where count1 = count2

私は、子供の 1 人のカウントが等しい親を取得します。すべての子のカウントが等しい親を取得するにはどうすればよいですか?

4

3 に答える 3

1

最初に派生テーブルを使用して、一致する行と行の数を取得します。

  select
    *
    from
    pc2
    join
    (
    select
    child_id,
    count1,
    count2,
    count(child_id) as NUM_ROWS,
    count(case when count1 = count2 then 1 else null) as NUM_MATCHES
    from child
    group by child_Id
    ) child
    on pc2.child_id = child.child_id
    where child.num_rows = num_matches
于 2013-11-06T20:23:53.803 に答える