SQL Select only rows where exact multiple relationships exist

后端 未结 10 1272
温柔的废话
温柔的废话 2020-12-06 02:06

This is closely related to this question, but adds another requirement.

Given a parent table \'parent\'

╔════════════╦════════╗
║ PARENT_ID  ║ NAME           


        
10条回答
  •  遥遥无期
    2020-12-06 02:54

    If MySql supported minus, the query could look like this:

    select parent_id
    from rel
    where prop_id in (5,1)
    group by parent_id
    having count(distinct prop_id)=2 and count(prop_id)=2
    minus
    select parent_id
    from rel
    where prop_id not in (5,1);
    

    The not in will remove those relationships that exceed (5,1), e.g. (5,1,3).

    I know you're using MySql and my answer hence is wrong. Just take it as another idea.

提交回复
热议问题