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

后端 未结 9 2352
刺人心
刺人心 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:09

    like the answer above but I have a duplicate record so I have to create a subquery with distinct

    Select user_id
    (
       select distinct userid
       from yourtable
       where user_id = @userid
    
    ) t1
    where 
    ancestry in ('England', 'France', 'Germany')
    group by user_id
    having count(user_id) = 3
    

    this is what I used because I have multiple record(download logs) and this checks that all the required files have been downloaded

    0 讨论(0)
  • 2020-11-30 06:11
    SELECT DISTINCT (user_id) 
    FROM [user]
    WHERE user.user_id In (select user_id from user where ancestry = 'England') 
        And user.user_id In (select user_id from user where ancestry = 'France') 
        And user.user_id In (select user_id from user where ancestry = 'Germany');`
    
    0 讨论(0)
  • 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'
    
    0 讨论(0)
  • 2020-11-30 06:14

    First way: JOIN:

    get people with multiple countries:

    SELECT u1.user_id 
    FROM users u1
    JOIN users u2
    on u1.user_id  = u2.user_id 
    AND u1.ancestry <> u2.ancestry
    

    Get people from 2 specific countries:

    SELECT u1.user_id 
    FROM users u1
    JOIN users u2
    on u1.user_id  = u2.user_id 
    WHERE u1.ancestry = 'Germany'
    AND u2.ancestry = 'France'
    

    For 3 countries... join three times. To only get the result(s) once, distinct.

    Second way: GROUP BY

    This will get users which have 3 lines (having...count) and then you specify which lines are permitted. Note that if you don't have a UNIQUE KEY on (user_id, ancestry), a user with 'id, england' that appears 3 times will also match... so it depends on your table structure and/or data.

    SELECT user_id 
    FROM users u1
    WHERE ancestry = 'Germany'
    OR ancestry = 'France'
    OR ancestry = 'England'
    GROUP BY user_id
    HAVING count(DISTINCT ancestry) = 3
    
    0 讨论(0)
  • 2020-11-30 06:14

    brute force (and only tested on an Oracle system, but I think this is pretty standard):

    select distinct usr_id from users where user_id in (
        select user_id from (
          Select user_id, Count(User_Id) As Cc
          From users 
          GROUP BY user_id
        ) Where Cc =3
      )
      and ancestry in ('England', 'France', 'Germany')
    ;
    

    edit: I like @HuckIt's answer even better.

    0 讨论(0)
  • 2020-11-30 06:16

    Try this:

    Select user_id
    from yourtable
    where ancestry in ('England', 'France', 'Germany')
    group by user_id
    having count(user_id) = 3
    

    The last line means the user's ancestry has all 3 countries.

    0 讨论(0)
提交回复
热议问题