phpmysqlsqlminnullif

using MIN and NULLIF correctly


$min_cost = min(NULLIF(value, 0))($cost_1, $cost_2, $cost_3, $cost_4);

Some costs on the database return $0.00, so that automatically becomes the $min_cost, even though there is values of more than zero on the other costs

I cant find much info on where the brackets should be on the NULLIF ? anyone help me?


Solution

  • If you don't want to get rid of the 0 entries through SQL, try using array_filter.

    array_filter() should get rid of the 0 entries

    $array1 = array($cost_1, $cost_2, $cost_3, $cost_4);
    
    $min_cost = min(array_filter($array1));