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

前端 未结 5 1926
傲寒
傲寒 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 23:05

    select personID from T1 group by personID having count(distinct stuffID) in (select count(distinct stuffID) from T2)
    

    select count(distinct stuffID) from T2 <-- Would give the total number of distinct countIDs

    group by personID having count(distinct stuffID) <-- after grouping by personId , counting the number of stuffIds that personId has.

    So both the counts should be equal to get the desired result.

提交回复
热议问题