如何透過使用 MySQL 的 SUBSTRING_INDEX() 函式將 IP 地址劃分為四個相應部分?
假設我們有一個名為“ipaddress”的表,其中包含 IP 地址,其在列“IP”中的值如下所示 -
mysql> Select * from ipaddress; +-----------------+ | ip | +-----------------+ | 192.128.0.5 | | 255.255.255.255 | | 192.0.255.255 | | 192.0.1.5 | +-----------------+ 4 rows in set (0.10 sec)
現在藉助以下查詢中的 SUBSTRING_INDEX() 函式,我們可以將 IP 地址劃分為四個部分 -
mysql> Select IP, SUBSTRING_INDEX(ip,'.',1)AS '1st Part', -> SUBSTRING_INDEX(SUBSTRING_INDEX(ip,'.',2),'.',-1)AS '2nd Part', -> SUBSTRING_INDEX(SUBSTRING_INDEX(ip,'.',-2),'.',1)AS '3rd Part', -> SUBSTRING_INDEX(ip,'.',-1)AS '4th Part' from ipaddress; +-----------------+----------+----------+----------+----------+ | IP | 1st Part | 2nd Part | 3rd Part | 4th Part | +-----------------+----------+----------+----------+----------+ | 192.128.0.5 | 192 | 128 | 0 | 5 | | 255.255.255.255 | 255 | 255 | 255 | 255 | | 192.0.255.255 | 192 | 0 | 255 | 255 | | 192.0.1.5 | 192 | 0 | 1 | 5 | +-----------------+----------+----------+----------+----------+ 4 rows in set (0.05 sec)
廣告