mysql提供了兩個方法來處理ip地址
inet_aton 把ip轉爲無符號整型(4-8位)
inet_ntoa 把整型的ip轉爲電地址
插入數據前,先用inet_aton把ip地址轉爲整型,能夠節省空間,由於char(15) 佔16字節。
顯示數據時,使用inet_ntoa把整型的ip地址轉爲電地址顯示便可。
例子:
CREATE TABLE user
(id
int(11) unsigned NOT NULL AUTO_INCREMENT,name
varchar(100) NOT NULL,ip
int(10) unsigned NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB;123456
插入幾條數據
INSERT INTO user
(id
, name
, ip
) VALUES
(2, 'Abby', inet_aton('192.168.1.1')),
(3, 'Daisy', inet_aton('172.16.11.66')),
(4, 'Christine', inet_aton('220.117.131.12'));1234
mysql> select * from user
;
+----+-----------+------------+
| id | name | ip |
+----+-----------+------------+
| 2 | Abby | 3232235777 |
| 3 | Daisy | 2886732610 |
| 4 | Christine | 3698688780 |
+----+-----------+------------+
查詢顯示爲電地址
mysql> select id,name,inet_ntoa(ip) as ip from user
;
+----+-----------+----------------+
| id | name | ip |
+----+-----------+----------------+
| 2 | Abby | 192.168.1.1 |
| 3 | Daisy | 172.16.11.66 |
| 4 | Christine | 220.117.131.12 |
+----+-----------+----------------+mysql