表-1IP
202.84.52.77
205.14.52.77.32
118.121.149.128
表-2开始IP                结束IP    区域
001.000.000.000 001.255.255.255 美国
002.006.190.056 002.006.190.063 英国
003.000.000.000 003.255.255.255 美国
005.163.066.080 005.163.066.095 瑞典想得到如下表,就是表1根据ip字段判断在表2哪个ip段范围内,然后得出所在区域IP                             区域
202.84.52.77                   广东
205.14.52.77.32                山西
118.121.149.128               .......

解决方案 »

  1.   

    /*  判断IP地址在表中两个IP段的位置  (爱新觉罗.毓华 2007-12-31 广东深圳 答csdn zyciis630)  原帖地址:http://topic.csdn.net/u/20071230/22/f4c19d7f-194d-47c9-9840-474ca79c782f.html  */    /*问题描述  比我我IP:220.113.49.43   然后我有如下的数据库   那么我如何正确的查询出我的IP是符合哪一个数据呢   StartIP     EndIP      Area   220.112.208.0  220.112.255.255 湖北省武汉市 长城宽带   220.113.0.0   220.113.48.255  北京市 长城宽带   220.113.49.0   220.113.63.255  广东省广州市 长城宽带   220.113.64.0   220.113.79.22  湖北省武汉市 长城宽带   220.113.79.23  220.113.79.23  湖北省宜昌市 长城宽带刘家大堰小区   220.113.79.24  220.113.81.208  湖北省武汉市 长城宽带   220.113.81.209  220.113.81.209  湖北省武汉市 (汉口)解放大道1511号名仕装饰工程有限公司   220.113.81.210  220.113.107.52  湖北省武汉市 长城宽带   220.113.107.53  220.113.107.53  湖北省武汉市 徐东路逸居苑小区   220.113.107.54  220.113.122.182 湖北省武汉市 长城宽带   220.113.122.183 220.113.122.183 湖北省武汉市 长城宽带湖北大学校内   220.113.122.184 220.113.127.255 湖北省武汉市 长城宽带  */    --------------------------------------------------------------  create table tb(StartIP varchar(50),EndIP varchar(50),Area varchar(100))  insert into tb values('220.112.208.0' ,'220.112.255.255','湖北省武汉市 长城宽带')   insert into tb values('220.113.0.0' ,'220.113.48.255' ,'北京市 长城宽带')   insert into tb values('220.113.49.0' ,'220.113.63.255' ,'广东省广州市 长城宽带')   insert into tb values('220.113.64.0' ,'220.113.79.22' ,'湖北省武汉市 长城宽带')   insert into tb values('220.113.79.23' ,'220.113.79.23' ,'湖北省宜昌市 长城宽带刘家大堰小区')   insert into tb values('220.113.79.24' ,'220.113.81.208' ,'湖北省武汉市 长城宽带')   insert into tb values('220.113.81.209' ,'220.113.81.209' ,'湖北省武汉市 (汉口)解放大道1511号名仕装饰工程有限公司 ')  insert into tb values('220.113.81.210' ,'220.113.107.52' ,'湖北省武汉市 长城宽带')   insert into tb values('220.113.107.53' ,'220.113.107.53' ,'湖北省武汉市 徐东路逸居苑小区')   insert into tb values('220.113.107.54' ,'220.113.122.182','湖北省武汉市 长城宽带')   insert into tb values('220.113.122.183','220.113.122.183','湖北省武汉市 长城宽带湖北大学校内')   insert into tb values('220.113.122.184','220.113.127.255','湖北省武汉市 长城宽带')  go    declare @ip as varchar(50)  set @ip = '220.113.49.43'    select startip , endip , area   from tb   where   cast(PARSENAME(@ip , 4) as bigint) * 256 * 256 * 256 + cast(PARSENAME(@ip , 3) as bigint) * 256 * 256 + cast(PARSENAME(@ip , 2) as bigint) * 256 + cast(PARSENAME(@ip , 1) as bigint) >=   cast(PARSENAME(startip , 4) as bigint) * 256 * 256 * 256 + cast(PARSENAME(startip , 3) as bigint) * 256 * 256 + cast(PARSENAME(startip , 2) as bigint) * 256 + cast(PARSENAME(startip , 1) as bigint) and   cast(PARSENAME(@ip , 4) as bigint) * 256 * 256 * 256 + cast(PARSENAME(@ip , 3) as bigint) * 256 * 256 + cast(PARSENAME(@ip , 2) as bigint) * 256 + cast(PARSENAME(@ip , 1) as bigint)  <=   cast(PARSENAME(endip , 4) as bigint) * 256 * 256 * 256 + cast(PARSENAME(endip , 3) as bigint) * 256 * 256 + cast(PARSENAME(endip , 2) as bigint) * 256 + cast(PARSENAME(endip , 1) as bigint)    drop table tb    /*  startip    endip     area  -------------- -------------- --------------------  220.113.49.0  220.113.63.255 广东省广州市 长城宽带    (1 行受影响)  */  ---- 
      

  2.   

    看下面的
    http://topic.csdn.net/u/20080414/16/48e0e77b-5a6c-49e8-a423-372cf42b77ff.html
      

  3.   

    http://topic.csdn.net/u/20080711/15/66639249-52d9-40c6-8f5c-131e49c1a6cf.html
      

  4.   

    点分格式的ip地址,你可以认为是256进制的4位数,
    根据各进制向十进制转换的规则:
    N=an*256^(n-1)+...a1*256^(1-1),
    可以知道,202.84.52.77的十进制数为
    202*256^3+84*256^2+52*256^1+77*256^0=3397910605
    然后再与表2开始结束IP作比较
    比如表2第一列
    开始IP:1*256^3+0*256^2+0*256^1+0*256^0= 16777216
    结束IP:1*256^3+255*256^2+255*256^1+255*256^0=33554431declare @ip as varchar(50)  
    set @ip = '220.113.49.43' 
      
    select  区域  from 表2  
     
    where   
    cast(PARSENAME(@ip , 4) as bigint) * 256 * 256 * 256 + 
    cast(PARSENAME(@ip , 3) as bigint) * 256 * 256 + 
    cast(PARSENAME(@ip , 2) as bigint) * 256 + 
    cast(PARSENAME(@ip , 1) as bigint)    
     >= 
    cast(PARSENAME(startip , 4) as bigint) * 256 * 256 * 256 + 
    cast(PARSENAME(startip , 3) as bigint) * 256 * 256 + 
    cast(PARSENAME(startip , 2) as bigint) * 256 + 
    cast(PARSENAME(startip , 1) as bigint) and  
     
    cast(PARSENAME(@ip , 4) as bigint) * 256 * 256 * 256 + 
    cast(PARSENAME(@ip , 3) as bigint) * 256 * 256 + 
    cast(PARSENAME(@ip , 2) as bigint) * 256 + 
    cast(PARSENAME(@ip , 1) as bigint)    
     <= 
    cast(PARSENAME(endip , 4) as bigint) * 256 * 256 * 256 + 
    cast(PARSENAME(endip , 3) as bigint) * 256 * 256 + 
    cast(PARSENAME(endip , 2) as bigint) * 256 + 
    cast(PARSENAME(endip , 1) as bigint)