2010-02-06 99 views
1

我有如下表:SQL集团选择查询

----------- ---------- ----------- 
| AccountID | Password | IpAddress | 
----------- ---------- -----------| 
| 1   1234  127.0.0.1 | 
| 2   123  127.0.0.1 | 
| 3   1234  127.0.0.1 | 
| 4   12  127.0.0.2 | 
| 5   123  127.0.0.2 | 
| 6   12  127.0.0.2 | 
| 7   1   127.0.0.2 | 
| 8   123  127.0.0.3 | 
| 9   123  127.0.0.3 | 
----------- ---------- ----------- 

我想通过ip地址从中选择分组accountIDs,密码和IpAddresses其中两个密码ipaddresses是相同的,有一个以上的ACCOUNTID。行超过1 accountids具有相同的密码和IP。 该表的结果是行1,3(ip group 1); 4,6(ip组2); 8,9(组3)。

谢谢。

回答

3

如果我理解正确的你,这是你想要的

select t1.* from(select password, IpAddress 
from YourTable 
group by password, IpAddress 
having count(*) > 1) t2 
join YourTable t1 on t1.IpAddress = t2.IpAddress 
and t1.password= t2.password