SQL选择多个ip用户

时间:2016-05-13 13:46:53

标签: mysql

我有user_idip

我需要拥有多个user_id

的IP

示例表

|user_id| |ip|
1, 127.0.0.1
2, 127.0.0.1
3, 82.12.12.12
3, 82.12.12.12
3, 92.15.15.15

所以我的结果必须是127.0.0.1

以下查询为我提供了多次使用的所有IP

SELECT ip, COUNT(user_id) 
  FROM user_login_histories 
  GROUP BY ip HAVING COUNT(DISTINCT(user_id)) > 1 
  ORDER BY ip

我缺少什么

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT ip, GROUP_CONCAT(DISTINCT user_id)
FROM user_login_histories 
GROUP BY ip 
HAVING COUNT(DISTINCT user_id) > 1
ORDER BY COUNT(DISTINCT user_id) DESC

答案 1 :(得分:1)

试试这个:

SELECT ip, COUNT(DISTINCT(user_id)) 
FROM user_login_histories 
GROUP BY ip
HAVING COUNT(DISTINCT(user_id)) > 1 
ORDER BY COUNT(DISTINCT(user_id))