Group values by a common value: userid and ipaddress

前端 未结 3 2025
说谎
说谎 2021-01-27 16:06

I\'m working on a problem in my database. I\'m trying to find the users who are using multiple accounts. I have a list of user IDs and used IP addresses, like this:

Us

3条回答
  •  攒了一身酷
    2021-01-27 16:22

    Taking the data from your example:

    +--------+---------+
    | userid | address |
    +--------+---------+
    | 1      | 12      |
    | 5      | 12      |
    | 1      | 13      |
    | 2      | 23      |
    | 9      | 23      |
    | 2      | 56      |
    | 4      | 56      |
    +--------+---------+
    

    for a table defined as

    CREATE TABLE table_27620030 (
        userid INTEGER,
        address INTEGER
    );
    

    the following query will list all userids that share the same address (and the address):

    SELECT address, GROUP_CONCAT(userid) userids
      FROM table_27620030
      GROUP BY address
      HAVING COUNT(DISTINCT userid) > 1
      ORDER BY address
    ;
    

    result:

    +---------+---------+
    | address | userids |
    +---------+---------+
    | 12      | 1,5     |
    | 23      | 2,9     |
    | 56      | 2,4     |
    +---------+---------+
    

提交回复
热议问题