我正试图找到一种方法来检测那些拥有重复帐户并从我的数据库中的表中订购的用户。我做了一些复杂的查询,没有解决我的问题。
第一个想法是按ip和count(user_id)分组,但是用户可以有多个订单,所以这些ip也会被计算在内(例如,如果我使用上面的,ip "192.168.1.1“将返回3,而不是我想要的2。
我的数据就像
| order_id | user_id | ip |
---------------------------------------
| 1001 | 2 | 192.168.1.1 |
| 1002 | 5 | 192.168.1.1 |
| 1003 | 2 | 192.168.1.1 |
| 1004 | 12 | 18.15.0.1 |
| 1005 | 9 | 10.0.0.1 | 结果需要IP 192.168.1.1,因为它有两个不同的user_id
任何帮助都是非常感谢的。
发布于 2015-02-26 15:51:18
您正在寻找提供给多个用户的IP?然后按IP分组,统计不同的用户。
select ip
from mytable
group by ip
having count(distinct user_id) > 1;编辑:使用户与之关联
select user_id, ip
from mytable
where ip in
(
select ip
from mytable
group by ip
having count(distinct user_id) > 1
);以下是存在子句的相同之处:
select user_id, ip
from mytable
where exists
(
select *
from mytable other
where other.ip = mytable.ip
and other.user_id <> mytable.user_id
);发布于 2015-02-26 15:49:43
在您的Distinct中使用Count
SELECT COUNT(Distinct user_ID)
FROM table
GROUP BY ip发布于 2015-02-26 15:49:03
试试这个:
SELECT t.ip, t.user_id,
(SELECT COUNT(1)
FROM yourtable t3 WHERE t.user_id = t3.user_id
AND t.ip = t3.ip) as total
FROM yourtable t
WHERE EXISTS(
SELECT 'duplicate'
FROM yourtable t2
WHERE t.user_id = t2.user_id
AND t.ip = t2.ip
AND t.order_id < t2.order_id
)https://stackoverflow.com/questions/28746683
复制相似问题