【mysql】IP地址整数int和varchar的转换
mysql中IP地址的存储
IP:如192.168.12.145,在存储时,若是采用varchar进行存储,存在两个主要缺点:
- 存储空间占用较大;
- 查询检索较慢;
解决方式:
- 存储时:将字符串类型的IP转换为整型进行存储;
- 查询时:将整型的IP转换为字符串;
Mysql自带的IP转换语句
- inet_aton:将ip地址转换成数字型
- inet_ntoa:将数字型转换成ip地址
示例1:
//使用inet_aton函数,将字符串IP转换为整型;
mysql> select inet_aton('73.115.134.73') as ip;
+------------+
| ip |
+------------+
| 1232307785 |
+------------+
//使用inet_ntoa函数,将整型IP转换为字符串;
mysql> select inet_ntoa(1232307785) as ip;
+---------------+
| ip |
+---------------+
| 73.115.134.73 |
+---------------+
示例2:
//不进行转换,查询结果为整型
mysql> select src_ip from natTable limit 5;
+------------+
| src_ip |
+------------+
| 1232307785 |
| 1232285337 |
| 1232323310 |
| 1232325234 |
| 1232326662 |
+------------+
//通过inet_ntoa函数进行转换,查询结果为IP格式的字符串
mysql> select inet_ntoa(src_ip) from natTable limit 5;
+-------------------+
| inet_ntoa(src_ip) |
+-------------------+
| 73.115.134.73 |
| 73.115.46.153 |
| 73.115.194.238 |
| 73.115.202.114 |
| 73.115.208.6 |
+-------------------+
自定义转换函数:
如:将1232307785转换为73.116.134.73
DELIMITER $$
CREATE FUNCTION natAndImDbTest11.ipBigIntToString (
ip bigint
)
RETURNS CHAR(15)
BEGIN
DECLARE o1 INT;
DECLARE o2 INT;
DECLARE o3 INT;
DECLARE o4 INT;
DECLARE ip_new_varchar VARCHAR(15);
IF (ip > 4294967295) then
RETURN '255.255.255.255';
END if;
IF (ip <= 0) then
RETURN '0.0.0.0' ;
END if;
SET o1 = ip / 16777216;
SET ip = ip % 16777216 ;
SET o2 = ip / 65536 ;
SET ip = ip % 65536 ;
SET o3 = ip / 256 ;
SET ip = ip % 256 ;
SET o4 = ip ;
SET ip_new_varchar = concat( cast(o1 as char(3)),'.',cast(o2 as char(3)),'.',cast(o3 as char(3)),'.',cast(o4 as char(3)));
RETURN (ip_new_varchar);
END;
$$
DELIMITER ;
测试:
use natAndImDbTest11;
mysql> select ipBigIntToString(1232307785);
+------------------------------+
| ipBigIntToString(1232307785) |
+------------------------------+
| 73.116.134.73 |
+------------------------------+
其他说明
删除自定义方法
drop FUNCTION if exists natAndImDbTest11.ipBigIntToString;
自定义方法的局限
本打算使用 ipBigIntToString 代替 inet_ntoa 进行查询,发现不行:
mysql> select ipBigIntToString(src_ip) from natTable limit 5;
ERROR 5 (HY000): The query includes syntax that is not supported by the Infobright Optimizer. Either restructure the query with supported syntax, or enable the MySQL Query Path in the brighthouse.ini file to execute the query with reduced performance.
mysql>
可能有其他方式可以使用ipBigIntToString替代inet_ntoa,但是目前自己未成功;
【mysql】IP地址整数int和varchar的转换的更多相关文章
- IP地址转换为Int
1.转换类 import com.google.common.base.Strings; import java.security.InvalidParameterException; import ...
- IP地址和int互转
/** * @author: yqq * @date: 2019/5/8 * @description: ip地址与int之间互换 * https://mp.weixin.qq.com/s?__biz ...
- PHP中IP地址与整型数字互相转换详解
这篇文章主要介绍了PHP中IP地址与整型数字互相转换详解,本文介绍了使用PHP函数ip2long与long2ip的使用,以及它们的BUG介绍,最后给出自己写的两个算法,需要的朋友可以参考下 IP转换成 ...
- IP地址在mysql的存储(IP地址和int的转换)
PHP echo ip2long('192.168.1.38'); 输出:3232235814 MYSQL SELECT INET_ATON('192.168.1.38'); 输出:323223581 ...
- IP地址的存储和使用
ip地址使用int类型存储,用INET_NTOA()和INET_ATON()转换 mysql'),inet_aton('127.0.0.1'); +-------------------------+ ...
- windows下获取IP地址的两种方法
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- 批处理快速更改ip地址
在各种网络中切换,windows更换ip地址步骤: 进入控制面板--网络和internet--网络和共享中心--理性适配器设置--然后找到网卡--进入属性--然后internet 协议--更改ip信 ...
- C#根据IP地址和子网掩码计算广播地址
using System.Net; /// <summary> /// 获得广播地址 /// </summary> /// <param name="ipAdd ...
- C#获取局域网中的所有正在使用的IP地址
方法不是很好. using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
随机推荐
- python学习之路02
1.python的数据类型有:Number String List Truple Sets Dictionary . 数字类型:int float bool complex 2.不可变数据:数字 字符 ...
- url参数和字典的相互转化
目标url:https://www.baidu.com/s?&wd=python&ie=utf-8 将字典转成url参数 使用urllib.parse的urlencode方法,将字典对 ...
- (22)bootstrap 初识 + Font Awesome(字体图标库)
bootstrap作用就是简化布局 bootstrap是基于JQ的,所以内部代码使用的是jq语法 所以要使用bs,必须先倒入 1.head标签内倒入bs的css文件 <link rel=&qu ...
- 转:《Javascript模块化编程》
(一):模块的写法 转载至:http://www.ruanyifeng.com/blog/2012/10/javascript_module.html (二):AMD规范 转载至:http://www ...
- whmcs之全民idc
http://manage.cn.resellerclub.com/servlet/LogoutPassServlet 原教程例子:http://sharebar.org/1594.html (该教程 ...
- LeetCode - Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- 02C++namespace命名空间
一.C++命名空间基本常识 所谓namespace,是指标识符的各种可见范围.C++标准程序库中的所有标识符都被定义于一个名为std的namespace中. 1.<iostream>和&l ...
- MySQL 的日期类型有5个,分别是: date、time、year、datetime、timestamp。
类型 字节 格式 用途 是否支持设置系统默认值 date 3 YYYY-MM-DD 日期值 不支持 time 3 HH:MM:SS 时间值或持续时间 不支持 year 1 YYYY 年份 不支持 da ...
- split与re.split/捕获分组和非捕获分组/startswith和endswith和fnmatch/finditer 笔记
split()对字符串进行划分: >>> a = 'a b c d' >>> a.split(' ') ['a', 'b', 'c', 'd'] 复杂一些可以使用r ...
- 在Ubuntu上使用noip动态域名的方法(ddns)
首先,注册一个noip.com的帐号. 注册的步骤见这篇教程:http://www.cnblogs.com/infopi/p/3991407.html 建立目录 第1行进入当前用户的home目录 第2 ...