【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 ...
随机推荐
- RESTful规范(二)
七 解析器 解析器的作用: -用来解析前台传过来的数据编码方式 urlencoded:form表单:name=lqz&age= formdata :上传文件:--dadfgdgag-- jso ...
- Blender 3D 打印插件Print Toolbox
Blender 3D Print Toolbox Statistics 统计,可以提算出模型的体积,可供打印备料参考. Error Checking 错误检查 Solid 检查模型是否完整正确,是否有 ...
- poj 2155 B - Matrix 二维树状数组
#include<iostream> #include<string> #include<string.h> #include<cstdio> usin ...
- PADS 脚本记录:关于 getObjects
PADS 脚本记录:关于 getObjects GetObjects(plogObjectTypeComponent, "ZBOM*", False) 返回的一个对象,所有 ZBO ...
- .Net Core 应用方向 图谱
.Net Core 应用方向 图谱, 如下图 : 大规模并行计算 是 大数据 和 人工智能 的 基础, 是 未来 大计算能力 的 基础, 网格计算 是 未来 大计算能力 的 一个 分支 . 所以, ...
- 查看加密的vba代码
查看加密的vba代码,可以使用这个工具,excel文件里面的宏代码一览无余. https://files.cnblogs.com/files/laoxia/PVP.zip
- spring中afterPropertiesSet方法与init-method配置描述
1. InitializingBean.afterPropertiesSet()Spring中InitializingBean接口类为bean提供了定义初始化方法的方式,它仅仅包含一个方法:after ...
- redis设计与实现-数据结构
1,redis存储有5种数据对象,有7种数据结构底层实现 2,sds简单字符串 不直接使用字符数组或是string 封装了长度变量,加快获得字符串长度 杜绝缓冲区溢出(拼接字符串的时候不会因为内存里连 ...
- mysql 5.45 以后需要 需要 安全套接字问题
错误异常:According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established ...
- SQL数据库简单操作
sql语言简介 (1)数据库是文件系统,使用标准sql对数据库进行操作 * 标准sql,在mysql里面使用语句,在oracle.db2都可以使用这个语句 (2)什么是sql * Structured ...