从数据库行中选择最低价格并忽略 null 或 0

发布于 2024-10-17 16:16:16 字数 857 浏览 5 评论 0原文

我有一个名为“价格”的数据库,其中有 12 列。

comp1、comp2、comp3、comp4、comp5、comp6 等。

然后我想运行一个查询来查找存储在与 id 匹配的行中的最低价格,但是我认为它将 null 视为最低值,因为我没有得到正确的答案。

无论如何?或者我做错了吗?

$query = "SELECT * FROM Prices WHERE id =$id" or die(mysql_error());
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($result)) {

    $comp1= $row['comp1'];
    $comp2= $row['comp2'];
    $comp3= $row['comp3'];
    $comp4= $row['comp4'];
    $comp5= $row['comp5'];
    $comp6= $row['comp6'];
    $comp7= $row['comp7'];
    $comp8= $row['comp8'];
    $comp9= $row['comp9'];
    $comp10= $row['comp10'];
    $comp11= $row['comp11'];
    $comp12= $row['comp12'];

$min = array( $comp1,  $comp2,  $comp3,  $comp4,  $comp5,  $comp6,  $comp7,  $comp8,  $comp8,  $comp10,  $comp11,  $comp12);

echo min($min);      

}

I have a database called prices and there are 12 columns lets say.

comp1, comp2, comp3, comp4, comp5, comp6 and so on.

I then want to run a query to find the lowest price stored in that row that matches an id, however I think it is treating null as lowest value as I am not getting the correct answer.

Anyway around this? or am i doing this wrong?

$query = "SELECT * FROM Prices WHERE id =$id" or die(mysql_error());
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($result)) {

    $comp1= $row['comp1'];
    $comp2= $row['comp2'];
    $comp3= $row['comp3'];
    $comp4= $row['comp4'];
    $comp5= $row['comp5'];
    $comp6= $row['comp6'];
    $comp7= $row['comp7'];
    $comp8= $row['comp8'];
    $comp9= $row['comp9'];
    $comp10= $row['comp10'];
    $comp11= $row['comp11'];
    $comp12= $row['comp12'];

$min = array( $comp1,  $comp2,  $comp3,  $comp4,  $comp5,  $comp6,  $comp7,  $comp8,  $comp8,  $comp10,  $comp11,  $comp12);

echo min($min);      

}

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

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

发布评论

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

评论(4

撞了怀 2024-10-24 16:16:16

如果您在调用 min() 之前的数组,您将清除任何计算结果为 false 的内容,包括 null0

例如:$min = min(array_filter($min));

If you call array_filter() on the array before calling min(), you will purge anything that evaluates to false, including null and 0.

For example: $min = min(array_filter($min));

夜吻♂芭芘 2024-10-24 16:16:16

尝试:

$min = array( $comp1,  $comp2,  $comp3,  $comp4,  $comp5,  $comp6,  $comp7,  $comp8,  $comp8,  $comp10,  $comp11,  $comp12);
$min = array_filter($min);

echo min($min);    

Try:

$min = array( $comp1,  $comp2,  $comp3,  $comp4,  $comp5,  $comp6,  $comp7,  $comp8,  $comp8,  $comp10,  $comp11,  $comp12);
$min = array_filter($min);

echo min($min);    
北城挽邺 2024-10-24 16:16:16

来自 http://php.net/manual/en/function.min.php 中的评论

我已经修改了无错误最小版本以忽略 NULL 值(否则返回 0)。

<?php
function min_mod () {
  $args = func_get_args();

  if (!count($args[0])) return false;
  else {
    $min = false;
    foreach ($args[0] AS $value) {
      if (is_numeric($value)) {
        $curval = floatval($value);
        if ($curval < $min || $min === false) $min = $curval;
      }
    }
  }

  return $min;  
}
?>

From a comment in http://php.net/manual/en/function.min.php:

I've modified the bugfree min-version to ignore NULL values (else it returns 0).

<?php
function min_mod () {
  $args = func_get_args();

  if (!count($args[0])) return false;
  else {
    $min = false;
    foreach ($args[0] AS $value) {
      if (is_numeric($value)) {
        $curval = floatval($value);
        if ($curval < $min || $min === false) $min = $curval;
      }
    }
  }

  return $min;  
}
?>
窗影残 2024-10-24 16:16:16

试试这个:

$query = "SELECT * FROM Prices WHERE id =$id" or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
    $i= 0;
    foreach ($row as $val) {
        if ($i == 0) {
            $tmpMin = $val;
        } else { 
            if ($tmpMin < $val) {
                $val = $tmpMin;    
            }
        }
    }

    unset($tmpMin);
    echo $tmpMin;
}

Try this:

$query = "SELECT * FROM Prices WHERE id =$id" or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
    $i= 0;
    foreach ($row as $val) {
        if ($i == 0) {
            $tmpMin = $val;
        } else { 
            if ($tmpMin < $val) {
                $val = $tmpMin;    
            }
        }
    }

    unset($tmpMin);
    echo $tmpMin;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文