SQL QUERY multiple search in one row to find data from another row in the same table

前端 未结 1 1273
臣服心动
臣服心动 2021-01-23 19:51

I need some help with some code

I have a database table called \"stuff\" and I have this info:

+------+-------------+---------------------+
| id   | memb         


        
相关标签:
1条回答
  • 2021-01-23 20:12

    The problem is called Relational Division.

    SELECT  group_id
    FROM    stuff
    WHERE   member_id IN (11,22,33)
    GROUP   BY group_id
    HAVING  COUNT(*) = 3
    
    • SQLFiddle Demo

    if member_id is not unique for every group_id, you need to have DISTINCT in order to count only unique values.

    SELECT  group_id
    FROM    stuff
    WHERE   member_id IN (11,22,33)
    GROUP   BY group_id
    HAVING  COUNT(DISTINCT member_id) = 3
    

    More variations on this link:

    • SQL of Relational Division
    0 讨论(0)
提交回复
热议问题