我想将相同的“干净”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)
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/