简体   繁体   English

使用PHP检查两个范围是否在数据库中相交

[英]Check if two ranges intersect in a database with php

I have a database that has 2 columns Left_From and Left_To I need to basically check both columns with each row in the database to see if there is any overlapping ranges. 我有一个包含2列Left_From和Left_To的数据库,我需要基本上检查数据库中每一行的两列,以查看是否存在任何重叠范围。 So lets say there are 37 rows returned I need to check each row 37 times. 所以可以说有37行返回,我需要检查每行37次。 I have tried array_intersect() with ranges and multiple loops. 我尝试了array_intersect()与范围和多个循环。 I have also tried BETWEEN in mysql but that does do what I need it to either. 我也在mysql中尝试过BETWEEN,但这确实可以满足我的需要。 When I tried the between method I have two loops that I thought would take these two 当我尝试之间的方法时,我有两个循环,我认为会采取这两个

        $newstart[$crow] = $row['LEFT_FROM'];
        $newend[$crow] = $row['LEFT_TO'];

and compare them to the new data that would be looped through 37 times because of $row2. 并将它们与由于$ row2而循环37次的新数据进行比较。

//attempt at BETWEEN
    $newstart = array();
        $newend = array();
        $crow = 0;
        while ($row = mysqli_fetch_array($get)) {
            $newstart[$crow] = $row['LEFT_FROM'];
            $newend[$crow] = $row['LEFT_TO'];

                while ($row2 = mysqli_fetch_array($get)) {

                    $newsql = "SELECT * FROM database+table WHERE tablename = 'something' AND
                    LEFT_FROM BETWEEN " . $newstart[$crow] . " AND " . $newend[$crow] . " OR  
                    LEFT_TO BETWEEN " . $newstart[$crow] . " AND " . $newend[$crow] . " OR ".
                    $newstart[$crow] . " BETWEEN " .  "LEFT_FROM" . " AND ". " LEFT_TO";
                    $result = mysqli_query($GLOBALS['Con'], $newsql);
                    if (!$result) {
                        echo "Error: " . mysqli_error($GLOBALS['Con']) . "<br>";
                    }
                    if (!empty($result)) {
                        echo "Overlap: ". "Row2 LEFT_FROM: " . $row2['LEFT_FROM'] . " NewStart: " . $newstart[$crow] . " Row2 LEFT_TO: " . $row2['LEFT_TO'] . " Newend: " . $newend[$crow] . "<br>" . "Crow: " . $crow . "<br>" ;
                    }
                }

        echo "Crow: " . $crow . "<br>";
            $crow++;
        }

atempt at array_intersect all variables in arrays are set to 0 在array_intersect处尝试将数组中的所有变量设置为0

$result = mysqli_query($GLOBALS['con'], $select);
$Rows = $result->num_rows;
$Rows2 = $Rows;
$Rows3 = $Rows;
$Rows4 = $Rows;
$Rows5 = $Rows;
$range1 = array();
$range2 = array();
$range3 = array();
$range4 = array();

while ($counter <= $Rows) {
    $range1[$crow] = $leftfrom[$add];
    $range2[$crow] = $leftto[$add];
    $counter++;
    $road++;
    $crow++;
}

while ($counter2 <= $Rows2) {
    $range3[$crow2] = $leftfrom[$add2];
    $range4[$crow2] = $leftto[$add2];
    $counter2++;
    $road3++;
    $crow2++;
}

$Combined1 = array();
$Combined1 = array();
while ($counter3 <= $Rows3) {
    $Combined1[$crow5] = range($range1[$crow3], $range2[$crow3]);
    $Combined2[$crow6] = range($range3[$crow3], $range4[$crow3]);
    $crow5++;
    $crow6++;
    $crow3++;
    $counter3++;
}

$check1 = 0;
while ($check1 <= $Rows4) {
$GLOBALS['check2'] = 0;
    $Rows6 = $GLOBALS['check2'] + 1;
    while ($GLOBALS['check2'] <= $Rows5) {

    $results = array_intersect($Combined1[$check1],$Combined2[$Rows6]);
    if ($results) {
        $start = reset($results);
        $end = end($results);
        echo "These are overlapping" . "<br>";
        echo "Start of overlap: " . $start . " Rows#: " . $check1 . " Bad Row: " . $GLOBALS['check2'];

        echo "<br>";
        echo "end of overlap: " . $end;
        echo "<br>" . $GLOBALS['check2'] ;
        $GLOBALS['check2']++;

    }else{
        echo "<br>" . $check1 . " No duplicates" . "<br>";
        $GLOBALS['check2']++;
        }
        }
    $check1++;

}

To check, if two number ranges overlap, you could use the simple test x1 < y2 AND y1 < x2 . 要检查两个数字范围是否重叠,可以使用简单测试x1 < y2 AND y1 < x2 This could be combined in a single sql statement. 可以将其合并在单个sql语句中。

SELECT
    t1.LEFT_FROM as LEFT_FROM_1,
    t1.LEFT_TO as LEFT_TO_1,
    t2.LEFT_FROM as LEFT_FROM_2,
    t2.LEFT_TO as LEFT_TO_2
FROM
    t t1, t t2
WHERE
    t1.LEFT_FROM < t2.LEFT_TO AND t2.LEFT_FROM < t1.LEFT_TO

This query would return all overlapping ranges. 该查询将返回所有重叠范围。

You don't need a loop in PHP, you can do it entirely by joining the table with itself. 您不需要PHP中的循环,您可以通过将表与其自身连接来完全完成循环。

SELECT *
FROM yourTable AS t1
JOIN yourTable AS t2 
ON t1.id < t2.id
AND (t1.left_from <= t2.left_to AND t2.left_from <= t1.left_to)

t1.id < t2.id keeps it from treating a row as overlapping with itself (and using < rather than != keeps it from showing the same pair of rows twice). t1.id < t2.id使它不会将一行视为与自身重叠(并且使用<而不是!=会使它避免两次显示同一对行)。 Replace id with the primary key of your table. 用表的主键替换id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM