比我我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 湖北省武汉市 长城宽带

解决方案 »

  1.   

    不是是判断我的IP在哪一个IP段中
    我看了别人的是:
    Startip: 1*256*256*256+1*256*256+0*256+0 = 16842752 (这个数字才是最终要放到数据库里) 
       
    Endip: 1*256*256*256+1*256*256+0*256+255 = 16843007 (这个数字才是最终要放到数据库里
    本篇文章来源于 站长中国 转载请以链接形式注明出处 网址:http://www.zzchn.com/edu/20071103/63186.shtml但我保存到数据库的方式是220.113.49.0         220.113.63.255这种方式
    所以问要怎么来查询呢
      

  2.   

    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','湖北省武汉市 长城宽带')
    godeclare @ip as varchar(50)
    set @ip = '220.113.49.43'select startip , endip , area 
    from tb 
    where
       cast(PARSENAME(@ip , 4) as int) >= cast(PARSENAME(startip , 4) as int) and cast(PARSENAME(@ip , 4) as int) <= cast(PARSENAME(endip , 4) as int) and
       cast(PARSENAME(@ip , 3) as int) >= cast(PARSENAME(startip , 3) as int) and cast(PARSENAME(@ip , 3) as int) <= cast(PARSENAME(endip , 3) as int) and
       cast(PARSENAME(@ip , 2) as int) >= cast(PARSENAME(startip , 2) as int) and cast(PARSENAME(@ip , 2) as int) <= cast(PARSENAME(endip , 2) as int) and
       cast(PARSENAME(@ip , 1) as int) >= cast(PARSENAME(startip , 1) as int) and cast(PARSENAME(@ip , 1) as int) <= cast(PARSENAME(endip , 1) as int) drop table tb/*
    startip          endip              area
    ---------------- ------------------ ---------------------
    220.113.49.0     220.113.63.255     广东省广州市 长城宽带(1 行受影响)
    */
      

  3.   

    晕,一下子没想到这个PARSENAME
    好!!
      

  4.   

    --这样就行了.
    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','湖北省武汉市 长城宽带')
    godeclare @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 行受影响)
    */
      

  5.   

    龟,
    CREATE FUNCTION dbo.f_IP2Int(
    @ip char(15)
    )RETURNS bigint
    AS
    BEGIN
    DECLARE @re bigint
    SET @re=0
    SELECT @re=@re+LEFT(@ip,CHARINDEX('.',@ip+'.')-1)*ID
    ,@ip=STUFF(@ip,1,CHARINDEX('.',@ip+'.'),'')
    FROM(
    SELECT ID=CAST(16777216 as bigint)
    UNION ALL SELECT 65536
    UNION ALL SELECT 256
    UNION ALL SELECT 1)a
    RETURN(@re)
    END
    GOdeclare @t table(StartIP varchar(15),EndIP varchar(15),Area varchar(100))
    insert @t select '220.112.208.0','220.112.255.255','湖北省武汉市   长城宽带 '
    insert @t select '220.113.0.0','220.113.48.255','北京市   长城宽带'
    insert @t select '220.113.49.0','220.113.63.255','广东省广州市   长城宽带 '
    insert @t select '220.113.64.0','220.113.79.22','湖北省武汉市   长城宽带' 
    insert @t select '220.113.79.23','220.113.79.23','湖北省宜昌市   长城宽带刘家大堰小区' 
    insert @t select '220.113.79.24','220.113.81.208','湖北省武汉市   长城宽带' 
    insert @t select '220.113.81.209','220.113.81.209','湖北省武汉市   (汉口)解放大道1511号名仕装饰工程有限公司' 
    insert @t select '220.113.81.210','220.113.107.52','湖北省武汉市   长城宽带 '
    insert @t select '220.113.107.53','220.113.107.53','湖北省武汉市   徐东路逸居苑小区' 
    insert @t select '220.113.107.54','220.113.122.182','湖北省武汉市   长城宽带' 
    insert @t select '220.113.122.183','220.113.122.183','湖北省武汉市   长城宽带湖北大学校内' 
    insert @t select '220.113.122.184','220.113.127.255','湖北省武汉市   长城宽带'
    declare @ip varchar(15)
    set @ip = '220.113.49.43'
    select * 
    from @t
    where dbo.f_IP2Int(@ip) between dbo.f_IP2Int(StartIP) and dbo.f_IP2Int(EndIP)
    drop function f_IP2Int/*
    StartIP         EndIP           Area                                                                                                 
    --------------- --------------- ---------------------------------------------------------------------------------------------------- 
    220.113.49.0    220.113.63.255  广东省广州市   长城宽带 (所影响的行数为 1 行)*/