如何检查BBDD中的任何范围之间的两个数字范围是否? php

发布于 2025-01-30 05:53:07 字数 650 浏览 3 评论 0原文

我的问题是:

我在数据库2个字段中,称为“ min_amount”和“ max_amount”,它们之间形成了一系列数字。

例如:最小值:0和最大:1000

我要做的是检查在插入新范围之前,它是否在另一个现有范围之间。

目前,我有这样的事情:

        foreach ($ranges as $range){ // $range is every row in table
            $min = $range[0]; //the min value of BBDD row
            $max = $range[1]; // the max value of BBDD row
            $imin = $input["min_amount"]; // My min value I want to insert
            $imax = $input["max_amount"]; // My max value I want to insert
            if ($imin < $max || $imax > $min){
                $inrange = true;
            }
        } 

您将如何解决这个问题?谢谢。

My question is the following:

I have in database 2 fields called "min_amount" and "max_amount" and between them they form a range of numbers.

For example: min: 0 and max: 1000

What I am trying to do is to check if, before inserting a new range, it is between another existing range.

At the moment I have something like this:

        foreach ($ranges as $range){ // $range is every row in table
            $min = $range[0]; //the min value of BBDD row
            $max = $range[1]; // the max value of BBDD row
            $imin = $input["min_amount"]; // My min value I want to insert
            $imax = $input["max_amount"]; // My max value I want to insert
            if ($imin < $max || $imax > $min){
                $inrange = true;
            }
        } 

How would you solve this? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请止步禁区 2025-02-06 05:53:07

看来您正在尝试检查所有现有范围的新范围。在这种情况下,您可以获得所有现有范围的最高最小值和最低值,并将它们与您的新范围进行比较:

$highestMinValue = max(array_column($range, 0));
$lowestMaxValue = min(array_column($range, 1));

if ($highestMinValue > $lowestMaxValue ) {
    //hopefully you don't have this, but think if it happens how will you handle it
}

$imin = $input["min_amount"];
$imax = $input["max_amount"];
if ($highestMinValue <= $imin) && ($imax <= $lowestMaxValue) {
    $inrange = true;
}

It looks like you are trying to check the new range against all the existing ranges. In that case you can get the highest min value and the lowest max value of all the existing ranges and compare them against your new range:

$highestMinValue = max(array_column($range, 0));
$lowestMaxValue = min(array_column($range, 1));

if ($highestMinValue > $lowestMaxValue ) {
    //hopefully you don't have this, but think if it happens how will you handle it
}

$imin = $input["min_amount"];
$imax = $input["max_amount"];
if ($highestMinValue <= $imin) && ($imax <= $lowestMaxValue) {
    $inrange = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文