check if a column contains ALL the values of another column - Mysql

前端 未结 5 1913
傲寒
傲寒 2020-12-03 22:15

Let\'s suppose I have a table T1 with people IDs and other stuff IDs, as the following

Table: T1
personID | stuffID 
    1    |    1
    1    |    2
    1            


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 22:52

    select personID
    from T1
    where stuffID in (select stuffID from t2)
    group by personID
    having count(distinct stuffID) = (select count(*) from t2)
    

    I.e pick a person's stuffids which are in T2, count them (distinct only), and verify same number as in t2.

提交回复
热议问题