2012-04-18 2 views
0

패턴으로 MySQL 검색을 그룹화하는 것과 관련하여 this question을 찾았지만 IP 주소로이 작업을 수행해야합니다. 처음 3 옥텟으로 IP 주소를 그룹화해야하지만이 작업을 수행하는 방법을 알 수는 없습니다.그룹 MySQL에서 REGEX로 IP 주소를 검색

감사합니다 여기에 유용 할 수 있습니다

+0

달성하려는 출력의 예를 제공 할 수 있습니까? 고유 한 처음 3 옥텟의 목록 만 원합니까? 아니면 다른 것을 따를 것입니까? – Tim

+0

@Tim - 예, 모든 IP 주소에 대한 처음 3 옥텟의 목록과 각 IP 주소의 개수. –

답변

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); 

는 도움이되기를 바랍니다.

+0

달콤한! 당신은이 MySQL n00b에 대한 남자들 사이의 신입니다! –