If you create a binary IP file, you can apply a mask to it that saves only the first 3 parts of your IP address. The mask looks like this:
11111111111111111111111100000000
So, in the form of numbers, it looks like this:
SELECT CONV('11111111111111111111111100000000' ,2,10) as mask;
If you perform bitwise AND at your IP address using this mask, you will get this result:
SELECT '1.0.207.199', INET_NTOA( INET_ATON( '1.0.207.199') & 4294967040 ) ;
INET_ATON converts ip (A) ddress (TO) a (N) umber and INET_NTOA vice versa (although the real value means ASCII TO Network, but I like this shofthand better ;-).
So you can group your IP addresses as follows:
SELECT INET_NTOA( INET_ATON( ip ) & 4294967040 ) AS ipgroup, COUNT(ip) AS count FROM ip_list GROUP BY ipgroup ORDER BY count DESC;
Hope this helps.
source share