根据我们的IP地址列表从DB-IP数据库中获取位置



我们从DB-IP.com.下载了这个带有位置/国家/地区的IP地址列表

他们拥有的结构化数据如下所示,正如你所看到的,这是一个范围,而不是单个IP。我们称之为IPAddress_table。

IPStart; IPEnd; Country; State; City
0.0.0.0 0.255.255.255   US  California  Los Angeles
1.0.0.0 1.0.0.255   AU  Queensland  South Brisbane
1.0.1.0 1.0.3.255   CN  Fujian  Fuzhou
1.0.4.0 1.0.7.255   AU  Victoria    Melbourne
1.0.8.0 1.0.15.255  CN  Guangdong   Guangzhou

在我们的数据库用户(user_table)中,我们有超过100000个具有IP地址的用户。

如何在MySQL中加入这两个表,因为IP地址源是范围起始端?

非常感谢您的反馈。

感谢

尝试以下操作:

Assuming table names are IPAddress_table and user_table
Assuming Field Names are:
user_table : IPAddress (and others), 
IPAddress_table: IPStart, IPEnd (and others)

SQL尝试:

SELECT user_table.*, IPAddress_table.*
FROM user_table 
  INNER JOIN IPAddress_table 
    ON INET_ATON(user_table.IPAddress) 
      BETWEEN INET_ATON(IPAddress_table.IPStart) 
        AND INET_ATON(IPAddress_table.IPEnd)

编辑:您可能想将其更改为LEFT JOIN,以获得所有用户,即使IP范围不适合。

最新更新