有一个IP地址对应物理地址的表,IP分为startIP和EndIP 两部分,加密了的
例如:startIP        EndIP           Country
61.55.164.55   61.55.164.81    aa市网通ADSL
61.55.164.82   61.55.164.82    bb市XX网吧
61.55.164.83   61.55.164.101   cc市yy区ADSL
... 但在查询过程中遇到问题:例如我在查询'218.18.159.177' 这个IP是属于什么地区的,如果用
select * from tableIP 
where '218.18.159.177' Between 起始IP and 结束IP搜索结果:
202.18.15.0   202.18.16.255          xx电信ADSL
202.18.158.0  202.18.167.255 yy市
202.176.0.0   202.183.255.255 CZ88.NET 实际上,我需要的是 202.18.158.0  202.18.167.255  yy市  这条记录.还需继续筛选.有没有更好的查询方法?

解决方案 »

  1.   

    改正:select * from tableIP 
    where '218.18.159.177' Between 起始IP and 结束IP 改为:select * from tableIP 
    where '218.18.159.177' Between startIP  and EndIP
      

  2.   

    select * from tableIP 
    where '218.18.159.177' Between startIP  and EndIP
    -----------------
    这就行了
      

  3.   

    分段找select * from tableIP 
    where left(startIP,len('218.18.159.'))=left('218.18.159.177',len('218.18.159.'))  and left(EndIP,len('218.18.159.'))=left('218.18.159.177',len('218.18.159.'))
    and cast(right(startIP,len('177')) as int)<=177
    and cast(right(EndIP,len('177')) as int)>=177
    union
    select * from tableIP 
    where left(startIP,len('218.18.'))=left('218.18.159.177',len('218.18.'))  and left(EndIP,len('218.18.'))=left('218.18.159.177',len('218.18.'))
    and cast(
    SUBSTRING(startIP,len('218.18.')+1,len(startIP)-len('218.18.')-1) 
    as decimal(18,3))<=159
    and cast(
    SUBSTRING(EndIP,len('218.18.')+1,len(EndIP)-len('218.18.')-1) 
    as decimal(18,3))>=159