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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2367152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:12:06  来源:igfitidea点击:

Find if number is between two numbers in different fields in a MySQL database

phpmysql

提问by Marcus

How would you format a query on a MySQL database via PHP to find if an IP address falls between two numbers in two different fields?

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

Numerical representation of IP address to find:

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

1265631252

Database format:

数据库格式:

 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"

回答by Jordan Running

A query like this:

像这样的查询:

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

Will return the row(s) for which the supplied number is between IP FROMand IP TO.

将返回提供的数字介于IP FROM和之间的行IP TO

回答by Jorge Ferreira

If you already have the DWORD equivalent of the IP address then the response from Jordanwill do the trick.

如果您已经拥有 IP 地址的 DWORD 等效项,那么来自 Jordan响应就可以解决问题。

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

If you need to, before executing the query, calculate the DWORD equivalent of the ip address you can do the following.

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

Given an ip address ain the form part1.part2.part3.part4then the DWORD equivalent can be calculated with the formula:

给定a表单中的 ip 地址,part1.part2.part3.part4则可以使用以下公式计算等效的 DWORD:

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

and then you can use dword(a)result in your SQL query.

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

Start with an IP address like 206.191.158.55.

以 IP 地址开头,例如206.191.158.55.

((206 * 256 + 191) * 256 + 158) * 256 + 55

The dword equivalent of the IP address will be the result. In this case 3468664375.

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

See more about DWORD equivalent of ip addresses here.

在此处查看有关 IP 地址的 DWORD 等效项的更多信息。

回答by Pascal MARTIN

What about something like this :

这样的事情怎么样:

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

i.e. get all the lines for which '1265631252' is between ip_fromand ip_to?

即获取“ 1265631252”在ip_from和之间的所有行ip_to