MySQL 查詢如何將 IP 地址從同一表的 varchar 列複製到整數?


為此,你可以使用 INET_ATON()。我們先建立一個 -

mysql> create table DemoTable1404
   -> (
   -> IpAddress varchar(40)
   -> );
Query OK, 0 rows affected (1.02 sec)

使用 insert 在表中插入一些記錄 -

mysql> insert into DemoTable1404 values('192.168.120.0');
Query OK, 1 row affected (0.43 sec)
mysql> insert into DemoTable1404 values('192.168.120.20');
Query OK, 1 row affected (0.60 sec)
mysql> insert into DemoTable1404 values('224.0.0.0');
Query OK, 1 row affected (0.42 sec)

使用 select 從表中顯示所有記錄 -

mysql> select * from DemoTable1404;

這將產生以下輸出 -

+----------------+
| IpAddress      |
+----------------+
| 192.168.120.0  |
| 192.168.120.20 |
| 224.0.0.0      |
+----------------+
3 rows in set (0.00 sec)

以下是將 IP 地址從 varchar 列複製到同一中的查詢 -

mysql> select inet_aton(IpAddress) from DemoTable1404;

這將產生以下輸出 -

+----------------------+
| inet_aton(IpAddress) |
+----------------------+
|           3232266240 |
|           3232266260 |
|           3758096384 |
+----------------------+
3 rows in set (0.00 sec)

不,讓我們證明轉換是否成功 -

mysql> select inet_ntoa('3232266240');

這將產生以下輸出 -

+-------------------------+
| inet_ntoa('3232266240') |
+-------------------------+
| 192.168.120.0           |
+-------------------------+
1 row in set (0.27 sec)

更新於: 11-11-2019

205 次瀏覽

開啟你的 職業生涯

完成課程即可取得認證

開始學習
廣告