在mysql表中找到重复的ip

时间:2015-02-27 13:24:43

标签: mysql

我有桌子"用户":

  +------+------+-----------+
  |  id  |  pid | ipaddress |
  +------+------+-----------+
  |  1   |   1  | 127.0.0.1 |
  +------+------+-----------+
  |  2   |   2  | 127.0.0.1 |
  +------+------+-----------+
  |  3   |   2  | 127.0.0.1 |
  +------+------+-----------+
  |  4   |   3  | 127.0.0.1 |
  +------+------+-----------+
  |  5   |   3  | 127.0.0.2 |
  +------+------+-----------+
  |  6   |   3  | 127.0.0.2 |
  +------+------+-----------+
  |  7   |   4  | 127.0.0.1 |
  +------+------+-----------+
  |  8   |   4  | 127.0.0.1 |
  +------+------+-----------+
  |  9   |   4  | 127.0.0.1 |
  +------+------+-----------+

需要选择重复的IP地址。

if ipaddress > 1 in 'pid' echo 'your ip is repeated 2 times';
if ipaddress > 2 in 'pid' echo 'your ip is repeated 3 times';
else 'your ip is unique';

允许在不同的pid'中重复ip。

2 个答案:

答案 0 :(得分:1)

您可以使用此查询:

SELECT 
  pid, 
  ipaddress, 
  COUNT(ipaddress) AS ipcount
FROM 
  yourtable
GROUP BY 
  ipaddress, pid
HAVING ipcount > 1

这将输出以下内容:

pid ipdaddress  ipcount
2   127.0.0.1   2
4   127.0.0.1   3
3   127.0.0.2   2

答案 1 :(得分:1)

你可以做点什么

选择

pid,
ipaddress,
case 
 when count(*) > 2 then 'ip is repeated more than 2 times'
 when count(*) = 2 then 'ip is repeated 2 times' 
 else 'unique'
end as message
from table_name
group by pid,ipaddress