Matching an IP address with an IP range?

Posted by Legend on Stack Overflow See other posts from Stack Overflow or by Legend
Published on 2010-04-24T03:05:15Z Indexed on 2010/04/24 3:13 UTC
Read the original article Hit count: 247

Filed under:
|
|
|
|

I have a MySQL table setup as follows:

+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| ipaddress_s   | varchar(15) | YES  | MUL | NULL    |                | 
| ipaddress_e   | varchar(16) | YES  |     | NULL    |                | 
+---------------+-------------+------+-----+---------+----------------+

where, ipaddress_s and ipaddress_e look something like: 4.100.159.0-4.100.159.255

Now is there a way I can actually get the row that contains a given IP address? For instance, given the IP address: "4.100.159.5", I want the above row to be returned. So I am trying for a query that looks something like this (but of course this is wrong because in the following I am considering IPs as strings):

SELECT * FROM ranges WHERE ipaddress_s<"4.100.159.5" AND ipaddress_e>"4.100.159.5"

Any suggestions?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about language-agnostic