MySQL check if an IP-address is in range?

后端 未结 2 951
一整个雨季
一整个雨季 2020-12-31 08:55

I have a table that contains two columns ipStart and ipEnd

These two fields contain a range of ip address. For example:

`ip         


        
相关标签:
2条回答
  • 2020-12-31 09:03

    Try this

    SELECT *
    FROM TABLE_NAME
    WHERE (INET_ATON("193.235.19.255") BETWEEN INET_ATON(ipStart) AND INET_ATON(ipEnd));
    
    0 讨论(0)
  • 2020-12-31 09:22

    To explain converting an ip address to a number which a few answers have relied on (and which I agree with).

    The ip address needs to be treated as one 32 bit number rather than 4 8 bit numbers

    For example the ip address

    193.235.18.0

    converted to binary is:-

    11000001.11101011.00010010.00000000

    Which you translate into (ie, take the dots out):-

    11000001111010110001001000000000

    Working that out you get:-

    1 * 2147483648 = 2147483648 
    1 * 1073741824 = 1073741824 
    0 * 536870912 = 0
    0 * 268435456 = 0
    0 * 134217728 = 0
    0 * 67108864 = 0
    0 * 33554432 = 0
    1 * 16777216 = 16777216 
    1 * 8388608 = 8388608 
    1 * 4194304 = 4194304 
    1 * 2097152 = 2097152 
    0 * 1048576 = 0
    1 * 524288 = 524288 
    0 * 262144 = 0
    1 * 131072 = 131072 
    1 * 65536 = 65536 
    0 * 32768 = 0
    0 * 16384 = 0
    0 * 8192 = 0
    1 * 4096 = 4096 
    0 * 2048 = 0
    0 * 1024 = 0
    1 * 512 = 512 
    0 * 256 = 0
    0 * 128 = 0
    0 * 64 = 0
    0 * 32 = 0
    0 * 16 = 0
    0 * 8 = 0
    0 * 4 = 0
    0 * 2 = 0
    0 * 1 = 0
    

    Adding those together you get 3253408256

    You an short cut that a bit by treating the original IP address as a base 256 number. So you have 0 units, 18 of 256s, 235 of 65536 (ie, 256 * 256) and 193 of (ie, 256 * 256 * 256)

    0 * 1 + 18 * 256 + 235 * 256 * 256 + 193 * 256 * 256 * 256

    INET_ATON function that is mentioned does this for you.

    0 讨论(0)
提交回复
热议问题