Does the IP address match the IP range?

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 this: 4.100.159.0-4.100.159.255

Now is there a way I can get a string containing a given IP address? For example, given the IP address: "4.100.159.5", I want the specified string to be returned. Therefore, I am trying to execute a query that looks something like this (but, of course, this is wrong, because in the following I consider IP as strings):

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

Any suggestions?

+3
source share
1 answer

INET_ATON (expr) ip- , .

http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-aton

SELECT * FROM ranges WHERE INET_ATON(ipaddress_s)<INET_ATON('4.100.159.0') AND INET_ATON(ipaddress_e)>INET_ATON('4.100.159.5')
+8

Source: https://habr.com/ru/post/1742558/


All Articles