English 中文(简体)
在MySQL的开端地址和终点地址的对比范围
原标题:Compare range of ip addresses with start and end ip address in MySQL

I have a MySQL table where I store IP ranges. It is setup in the way that I have the start address stored as a long, and the end address (and an id and some other data). Now I have users adding ranges by inputting a start and end ip address, and I would like to check if the new range is not already (partially) in the database.

I know I can do a between query, but that doesn t seem to work with 2 different columns, and I also cannot figure out how to pass a range to compare it. Doing it in a loop in PHP is a possibility, but would with a range of e.g. 132.0.0.0-199.0.0.0 be quite a big amount of queries..

最佳回答

当你说这些地址是long时,我假定你指的是把这些地址储存起来,(say)10.1.2.3将储存为0x0a010203。 在这种情况下,如果发现地址已经存在,你可以做到:

SELECT ...
FROM ipranges
WHERE (<NEWADDR> >= startaddr)
    AND (<NEWADDR> <= endaddr)

如果你回头来,地址已经摆在桌面上。 (replace <NEWADDR> with the new Address, 当然!)

在检查重叠的浏览量方面,这只是稍加复杂:

SELECT ...
FROM ipranges
WHERE NOT ((<NEWENDADDR> < startaddr) OR (<NEWSTARTADDR> > endaddr))

i. 即,如果新范围从后开始,或者在其之前结束,则新范围就没有重叠。

问题回答

两者都是起步的:处理和最终解决时间长? 那么,为什么不把你重新寻找一个漫长的手法改为“条码”。 WHERE开始_address <=$myip and end_address >=$myip

就范围而言,在条款的三度上,情况就是如此。

WHERE (start_address <= $startAddress AND end_address >= $startAddress) 
   OR (start_address <= $endAddress AND end_address >= $endAddress) 
   OR (start_address >= $startAddress AND end_address <= $endAddress)

第一组发现的范围包括起始地址。 第二种情况是涵盖最终地址的范围。 这意味着,目前仍有可能将所投入的范围放在b中。 这是第三点检查。

这应当使你回到贯穿你们投入范围的所有范围......

如何储存IP添加物,其含义是,你可以采用BETWEEN进行简单的检查。

4294967295 - 4294967040 -> FFFF00 - FFFFFF-> 255.255.255.0 - 255.255.255

诸如:

SELECT * FROM addresses WHERE
    (start < $new_start AND end > $new_start) OR
    (start > $new_end AND end < $new_end);

这应当给你所有现有地址,这些地址将重叠。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签