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

前端 未结 4 1400
迷失自我
迷失自我 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 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.

提交回复
热议问题