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

前端 未结 4 1381
迷失自我
迷失自我 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:47
    select document_id from table1
     where subject_id in (17, 76)
     group by document_id
    having count(distinct subject_id) = 2
    
    0 讨论(0)
  • 2020-12-09 05:52

    I assume that the natrual key of this table is document_id + subject_id, and that id is a surrogate; IOW, document_id and subject_id are unique. As such, I'm just going to pretend it doesn't exist and that a unique constraint is on the natural key.

    Let's start with the obvious.

    SELECT document_id, subject_id
      FROM document_subjects
     WHERE subject_id IN (17,76)
    

    That gets you everything you want plus stuff you don't want. So all we need to do is filter out the other stuff. The "other stuff" is groups of rows having a count that is not equal to the count of the desired subjects.

    SELECT document_id
      FROM document_subjects
     WHERE subject_id IN (17,76)
     GROUP BY document_id
    HAVING COUNT(*) = 2
    

    Note that subject_id is removed because it doesn't participate in grouping. Taking this one step further, i'm going to add an imaginary table called subjects_i_want that contains N rows of subjects you want.

    SELECT document_id
      FROM document_subjects
     WHERE subject_id IN (SELECT subject_id FROM subjects_i_want)
     GROUP BY document_id
    HAVING COUNT(*) = (SELECT COUNT(*) FROM subjects_i_want)
    

    Obviously subjects_i_want could be swapped out for another subquery, temporary table, or whatever. But, once you have this list of document_id, you can use it within a subselect of a bigger query.

    SELECT document_id, subject_id, ...
      FROM document_subjects
     WHERE document_id IN(
            SELECT document_id
              FROM document_subjects
              WHERE subject_id IN (SELECT subject_id FROM subjects_i_want)
              GROUP BY document_id
             HAVING COUNT(*) = (SELECT COUNT(*) FROM subjects_i_want))
    

    Or whatever.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-09 06:01

    That's a very interesting question.

    I'm assuming you would like a more generalized query, but this is what I would do in the case where you always have the same number of subjects (say two):

     SELECT T.id, T.document_id, T.subject_id
       FROM table T
            INNER JOIN table T1 ON T.document_id = T1.document_id AND T1.subject_ID = 17
            INNER JOIN table T2 ON T.document_id = T2.document_id AND T2.subject_ID = 76            
    

    Of course, you could add yet another INNER JOIN to add another subject ID.. But I admit it's not a very good general solution.

    0 讨论(0)
提交回复
热议问题