日期:2014-05-18  浏览次数:20471 次

纯真IP的使用问题.
表结构:
startip   值:222.35.82.0
endip       值:222.35.95.255
contry     值:北京....
startid   值:3.726857E+09
endid       值:3.72686E+09

请问是如何匹配IP段的?

我用:
SET   QUOTED_IDENTIFIER   ON  
GO
SET   ANSI_NULLS   ON  
GO

ALTER     FUNCTION   [dbo].[IPLong]  
(@IP   varchar(20)
)
RETURNS   bigint
AS
BEGIN
  declare   @IPLong   as   bigint
  if   @IP   =   ' '  
  begin
    set   @IPLong=0
  end
  else
  begin
    declare   @No   int
    set   @No=3
    set   @IPLong=0
    declare   @temp   varchar(15)
    set   @temp=@ip
    while   len(@temp)> 0   and   charindex( '. ',@temp)> 0
    begin
      set   @IPLong=@IPlong   +   cast(substring(@temp,1,charindex( '. ',@temp)-1)   as   bigint)*power(256,@No)
      set   @temp=substring(@temp,charindex( '. ',@temp)+1,len(@temp)-charindex( '. ',@temp))
      set   @No=@No-1
    end
    set   @IpLong   =   @IpLong   +   cast(@temp   as   bigint)
  end
  return   @IpLong
END


GO
SET   QUOTED_IDENTIFIER   OFF  
GO
SET   ANSI_NULLS   ON  
GO

然后查:
select   *   from   ip  
where   startid   >   dbo.IPLong( '123.117.88.200 ')
and   endid <dbo.IPLong( '123.117.88.200 ')

查不出记录.
请问大家是如何判断的?


------解决方案--------------------
你的startid和endid是同一个值是查不出数据的,最起码应该加个=
select * from ip
where startid > = dbo.IPLong( '123.117.88.200 ')
and endid <=dbo.IPLong( '123.117.88.200 ')
------解决方案--------------------
在一个范围查的话,起始ip和结束ip应该不相同
------解决方案--------------------
首先检查ip纯真数据库里的内容,起始ip和终止ip有没有相等的。
其次建个函数,转成256位,再次进行查询。对起始256位和终止256位的进行查找,没什么复杂的。我当时写个记录在:
http://blog.csdn.net/hrb2008/archive/2007/03/30/1546758.aspx