1

I have ip_table as below. Suppose my IP address is 1.22.13.15. But in my table this exact IP address is not available, This IP address is in the range between 0/24. How can I get the geoname_id for this IP address? Can someone assist me? Or there is any other way?

+---------------+------------+
| ip_adr        | geoname_id |
+---------------+------------+
| 1.22.13.0/24  |    1279233 |
| 1.22.130.0/23 |    1273294 |
| 1.22.132.0/23 |    1267995 |
| 1.22.134.0/23 |    1253405 |
| 1.22.136.0/21 |    1269750 |
+---------------+------------+
Talk2Nit
  • 1,115
  • 3
  • 22
  • 38

6 Answers6

1

You can use RegEx in SQL to identify the format of IP and check if the selected IP is in the range you give.

Example:

SELECT *
FROM ips
WHERE (ip_addr REGEXP '^[0-9]{1}\.[0-9]{2}\.[0-9]{2,3}\.0\/(24)')
Salim Ibrohimi
  • 1,351
  • 3
  • 17
  • 35
Andrei Todorut
  • 4,260
  • 2
  • 17
  • 28
1

As far as I know there is no MySQL function that can deal with the range syntax for IP adresses directly. But MySQL has a function to convert IP adresses in a bigint number: INET_ATON()

one possible solution would be to extend the table structure in such a way that you can use simple queries:

|ip_adr_range_start  |ip_adr_range_end | geoname_id|
|[bigint]            |[bigint]         | [int?]

the first row would be inserted like this:

INSERT INTO your_table VALUES (INET_ATON('1.22.13.0'), INET_ATON('1.22.13.255'), 1279233);

a query could be:

SELECT * from your_table 
WHERE ip_adr_range_start < INET_ATON('1.22.13.15') 
AND ip_adr_range_end > INET_ATON('1.22.13.15')

ideas taken from here: https://dba.stackexchange.com/questions/36451/convert-the-ip-address-range-to-two-bigint-for-faster-lookup

cypherabe
  • 2,562
  • 1
  • 20
  • 35
1

try Substring_index

select left('1.22.13.0/24',length('1.22.13.0/24')- length(substring_index('1.22.13.0/24','.',-1))) part1,substring_index(substring_index('1.22.13.0/24','.',-1),'/',1) as range_start,substring_index('1.22.13.0/24','/',-1) range_end

the output will be

part1 | range_start| range_end

1.22.13.| 0 | 24

then you can compare your value accordingly

Vishal Gupta
  • 326
  • 1
  • 7
0

I guess this table mean the ip in range between 1.22.13.0-1.22.13.255 will match with 1279233 geonameid. You need to define IP range by subnet provided. So, to get a geonameid with specific ip, just ask it what are its IP and subnet? How to compare the specific ip address with ip range and compare with geonameid? More info at How to check an IP address is within a range of two IPs in PHP?

sycoi001
  • 74
  • 1
  • 8
0

I got the output using the following query.

I split my IP address into two parts. Like 1.22.13 and 15 passed these in where condition.

SELECT ip_adr, geoname_id
FROM ip_table 
WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(ip_adr, '/', 1 ), '.', 3) = 
'1.22.13' AND
SUBSTRING_INDEX(SUBSTRING_INDEX(ip_adr, '/', 1 ), '.', -1) <= '15';
Talk2Nit
  • 1,115
  • 3
  • 22
  • 38
0
with ipaddressTBL as(
    select *,  INET_ATON(ip) as ipaddress from `routers` 
) select * from ipaddressTBL 
where ipaddress BETWEEN INET_ATON('32.248.135.197') AND INET_ATON('165.9.105.127')
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197