MySQL将ip地址从varchar列复制到同一个表中的整数

标签 mysql

我想将相同的“干净”IP 编号从 varchar(32) 列复制到整数 1。

我尝试过这个:

  UPDATE table SET ipnum = REPLACE(ip, '.', '');

但是对于某些值,我得到的数字与原始“干净”的数字不同。

我肯定做错了什么,但不知道是什么。

现在的工作原理示例:

ip  varchar(32) numip int(11)
-----------------------------
41.220.68.241   2147483647
41.58.115.152   2147483647
41.220.68.13    412206813

我希望输出是这样的:

ip  varchar(32) numip int(11)
-----------------------------
41.220.68.241   4122068241  
41.58.115.152   4158115152
41.220.68.13    412206813

最佳答案

您可能应该使用内置功能:

mysql> select INET_ATON('10.0.0.1');
+-----------------------+
| INET_ATON('10.0.0.1') |
+-----------------------+
|             167772161 |
+-----------------------+
1 row in set (0.00 sec)

12.15 Miscellaneous Functions

Given the dotted-quad representation of an IPv4 network address as a string, returns an integer that represents the numeric value of the address in network byte order (big endian). INET_ATON() returns NULL if it does not understand its argument.

您可以使用INET_NTOA反转该过程,例如:

mysql> select INET_NTOA('167772161');
+------------------------+
| INET_NTOA('167772161') |
+------------------------+
| 10.0.0.1               |
+------------------------+
1 row in set (0.00 sec)

关于MySQL将ip地址从varchar列复制到同一个表中的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30495741/

相关文章:

Eclipse 中未检测到 Java MySQL 连接器

MYSQL查询更新三张表的数据

MySQL 安全和 symfony2

mysql - SQL 将 select 中的内容替换为左连接

java - 在创建连接池时为mysql配置时间戳

mysql - 哪个更好 : calling procedure inside a procedure or executing sql inside a procedure?

mysql - 在sql中乘以2行

java - Spring JPA - 当数据库中有数据时,为什么我的 findAll 返回 null?

php - 如果在更新多行时它不存在,我如何插入一行?

mysql - 多列上的全文索引如何工作?