mysql中操作IP地址的函数

来源:互联网 发布:网络平台贷款是否合法 编辑:程序博客网 时间:2024/04/30 11:40

把IP地址转换成bigint

CREATE FUNCTION `F_Ip2Int`(ip varchar(15)) RETURNS bigint(20)
BEGIN
  
declare tmp bigint default 0;
  
while instr(ip,'.')>0 do
    
set tmp = tmp*256+ left(ip,instr(ip,'.')-1);
    
set ip = right(ip,length(ip)-instr(ip,'.'));
  
end while;
  
set tmp = tmp*256+ip;
  
return tmp;
END

 把bigint转换成IP地址

CREATE FUNCTION `F_Int2Ip`(iip bigintRETURNS varchar(15)
BEGIN

  
return concat((iip & 0xFF000000)>>24'.',
                (iip 
& 0x00FF0000)>>16'.',
                (iip 
& 0x0000FF00)>>8'.',
                iip 
& 0x000000FF);

END
原创粉丝点击