I need to group IP list by subnet (first 3 octets) to count it. For example, if I have Ips
123.12.12.12
123.12.12.11
123.12.11.11
I have to get such result:
123.12.12 | 2
123.12.11 | 1
I googled this expample:
select
substr(ip,1,locate('.',ip,locate('.',ip)+1)-1)
as ip, count(ip) as count
from ip_list
group by ip ORDER BY count DESC
But it groups the list only by first two octets. I was lost in all these locate(locate(locate(...))). Can somebody help to modify this to get proper results?
You should have used
group byexpression name.EDIT 1:
Use of
locateis not required.SUBSTR_INDEXcan be used to filter the subset of IP’s.Example:
Refer to Documentation:
position pos.
delimiter delim.