查找数字是否在MySQL数据库中不同字段中的两个数字之间

时间:2010-03-02 22:00:23

标签: php mysql

如何通过PHP格式化MySQL数据库上的查询,以查找IP地址是否介于两个不同字段中的两个数字之间?

要查找的IP地址的数字表示:

1265631252

数据库格式:

 IP FROM      IP TO       REGISTRY  ASSIGNED   CTRY CNTRY COUNTRY
"1265631232","1265893375","arin","1152835200","US","USA","United States"
"1265893376","1265958911","arin","1149120000","US","USA","United States"
"1265958912","1266024447","arin","1149120000","US","USA","United States"
"1266024448","1266089983","arin","1162425600","US","USA","United States"

3 个答案:

答案 0 :(得分:33)

像这样的查询:

SELECT * FROM your_table WHERE 1265631252 BETWEEN `IP FROM` AND `IP TO`;

将返回所提供号码介于IP FROMIP TO之间的行。

答案 1 :(得分:3)

如果你已经拥有相当于IP地址的DWORD,那么response from Jordan就可以了。

SELECT * FROM your_table WHERE 1265631252 BETWEEN `IP FROM` AND `IP TO`;

如果在执行查询之前需要计算ip地址的DWORD,可以执行以下操作。

给定a形式的IP地址part1.part2.part3.part4,可以使用以下公式计算DWORD等效值:

dword(a) = ((a.part1 * 256 + a.part2) * 256 + a.part3) * 256 + a.part4

然后您可以在SQL查询中使用dword(a)结果。

206.191.158.55

等IP地址开头
((206 * 256 + 191) * 256 + 158) * 256 + 55

结果将是IP地址的等效词。在这种情况下3468664375

详细了解DWORD等效的IP地址here

答案 2 :(得分:2)

这样的事情:

select *
from your_table
where ip_from <= '1265631252'
  and ip_to >= '1265631252'

即。得到“1265631252”介于ip_fromip_to之间的所有行?