0

I have a database with Country-IP addresses.

My table has two fields.

LBound, tinyint
UBound, tinyint

When I query the table like this:

select * from allowed where '129.250.224.1' BETWEEN lbound AND ubound

I get 3 results:

12.205.104.0    13.16.137.9
129.250.2.43    129.250.3.137
129.250.16.47   129.253.255.255

But I only want to return:

129.250.16.47   129.253.255.255

Is this a data type issue? If so, what datatype should I be using?

juergen d
  • 201,996
  • 37
  • 293
  • 362
RickyD
  • 37
  • 1
  • 8

2 Answers2

1

You could use

http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html

 INETT_ATON 

and

  INET_NTOA

eg :

 SELECT INET_ATON('10.0.5.9');
    -> 167773449

In this way you can convert the IP in a number properly and filter correctly

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

You can use inet_aton function to convert IP address text into numeric value and compare. Like this:

SELECT *
FROM allowed
WHERE inet_aton('129.250.224.1') BETWEEN inet_aton(lbound) AND inet_aton(ubound);

Regarding which type to use to store the IP address, I'd use UNSIGNED INT or VARBINARY(16).


Related question -

Community
  • 1
  • 1
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76