SQL: how to select a single id (“row”) that meets multiple criteria from a single column

后端 未结 9 2362
刺人心
刺人心 2020-11-30 05:38

I have a very narrow table: user_id, ancestry.

The user_id column is self explanatory.

The ancestry column contains the country from where the user\'s ancest

9条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 06:13

    Users who have one of the 3 countries

    SELECT DISTINCT user_id
    FROM table
    WHERE ancestry IN('England','France','Germany')
    

    Users who have all 3 countries

    SELECT DISTINCT A.userID
    FROM table A
       INNER JOIN table B on A.user_id = B.user_id
       INNER JOIN table C on A.user_id = C.user_id
    WHERE A.ancestry = 'England'
       AND B.ancestry = 'Germany'
       AND C.ancestry = 'France'
    

提交回复
热议问题