Mysql
 sql >> Datenbank >  >> RDS >> Mysql

SQL:Binär zu IP-Adresse

mysql> select inet_ntoa(conv('4333d26e', 16, 10));
+-------------------------------------+
| inet_ntoa(conv('4333d26e', 16, 10)) |
+-------------------------------------+
| 67.51.210.110                       |
+-------------------------------------+
1 row in set (0.00 sec)

Prüfen Sie, ob es dort auch funktioniert =)

Bearbeiten

Das Problem ist, dass inet_ntoa scheint von dezimal zu parsen strings Zahlendarstellung, nicht hexadezimale, oder aus hexadezimalen integers . Vergleichen Sie:

mysql> select inet_ntoa(0x4333d26e);
+-----------------------+
| inet_ntoa(0x4333d26e) |
+-----------------------+
| 67.51.210.110         |
+-----------------------+
1 row in set (0.02 sec)

mysql> select inet_ntoa('0x4333d26e');
+-------------------------+
| inet_ntoa('0x4333d26e') |
+-------------------------+
| 0.0.0.0                 |
+-------------------------+
1 row in set, 1 warning (0.00 sec)

Bearbeiten

Das ist einfacher und scheint auch zu funktionieren:

SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata