SQL selecting rows where one column's value is common across another criteria column

前端 未结 4 1397
迷失自我
迷失自我 2020-12-09 05:23

I have a cross reference table that looks like this:

id  document_id  subject_id
1   8            21
2   5            17
3   5            76
4   7                    


        
4条回答
  •  不知归路
    2020-12-09 05:56

    Using Oracle (or any database that allows the with clause). This allows definition of the subject_id values exactly once.

    with t as (select distinct document_id from table1 where subject_id in (17,76) )
    select document_id from table1 where subject_id in (select subject_id from t)
    group by document_id 
    having count(*) = (select count (*) from t);
    

提交回复
热议问题