我发现this question关于按模式对MySQL搜索进行分组,但我需要为IP地址执行此操作。我需要在前3个八位字节对IP地址进行分组,但我不太清楚如何使用它。
由于
答案 0 :(得分:2)
MySQL SUBSTRING_INDEX function在这里可能很有用:
drop table if exists test_ip;
create table test_ip
(id int unsigned not null primary key auto_increment,
ip varchar(50) not null,
UNIQUE KEY test_ip_uidx1 (ip));
insert into test_ip (ip) values ('24.21.114.4');
insert into test_ip (ip) values ('24.21.114.5');
insert into test_ip (ip) values ('24.21.114.6');
insert into test_ip (ip) values ('24.21.115.6');
insert into test_ip (ip) values ('24.21.115.7');
insert into test_ip (ip) values ('24.21.115.8');
insert into test_ip (ip) values ('24.21.116.1');
select substring_index(ti.ip,'.',3) as firstThreeOctet,count(*) as ipCount
from test_ip ti
group by substring_index(ti.ip,'.',3);
希望它有所帮助。