在MySQL表中仅在字段不为空时选择''

时间:2015-06-29 02:53:38

标签: php mysql

我的大多数值已填充,但set_price列的空字段很少。我想只在set_price不为空时选择''。我使用的mysql和数据类型设置为set_price varchar(50)NOT NULL,

我的功能:

 function getAvilableServiceDetails($service_id,$select_from,$select_to,$select_weight,$dimesion_height,$dimesion_width,$dimesion_girth,$price_shorting)
            {
                global $objSmarty;  

        //  echo $service_id."//".$select_from."//".$select_to."//".$select_weight."//".$dimesion_height."//".$dimesion_width."//".$dimesion_girth."//".$price_shorting; exit;

                if($select_weight=='')
                    $select_weight=0;

                if($dimesion_height!='')
                $height_condition=" and  dimesion_height>=".$dimesion_height;
                else
                $height_condition="";

                if($dimesion_width!='')
                $width_condition="  and  dimesion_width>=".$dimesion_width;
                else
                $width_condition="";

                if($dimesion_girth!='')
                $girth_condition=" and dimesion_girth>=".$dimesion_girth;
                else
                $girth_condition="";



                //===================searching condition=========================//
                    if($_SESSION['package_search_by']!='')
                        $searching_condition=" and service_type='".$_SESSION['package_search_by']."'";
                    else
                        $searching_condition="";    

                //=====================price shorting============================//
                if($price_shorting!='')
                {
                    if($price_shorting=='L_TO_H')
                        $order_by=" group by unique_id order by set_price ASC ";
                    else    
                        $order_by=" group by unique_id order by set_price DESC ";
                }
                else
                {
                    $order_by=" group by unique_id order by set_price ASC ";
                }

                if($select_from!='' && $select_to!='' && $service_id!='')
                {
                   $select_query="select * from set_delivery_package_details  where service_id='".$service_id."' and 
                    dimesion_weight>=".$select_weight." and set_price IS NOT NULL and set_price<>'' and FIND_IN_SET ('".$select_from."',sel_countrys) and FIND_IN_SET ('".$select_to."',sel_countrys)  and deleted='N' and status='Y' ".$searching_condition.$height_condition.$width_condition.$girth_condition.$order_by;
                    $this->dbh->Query($select_query);
                    if($this->dbh->num_rows>0)
                    {
                        $PackageData=$this->dbh->FetchAllResults($select_query);
                        $objSmarty->assign("PackageData", $PackageData);
                    }
                    else
                    {
                        $searching_error_message="Record Not Found";
                        $objSmarty->assign("searching_error_message", $searching_error_message);
                    }


                }
                else
                {
                    $searching_error_message="Record Not Found Please Search Again";
                    $objSmarty->assign("searching_error_message", $searching_error_message);
                }
            }

我正在使用的表格: please click here

2 个答案:

答案 0 :(得分:0)

希望这项工作!

$select_query = 
    "SELECT * 
     FROM set_delivery_package_details  
     WHERE service_id='".$service_id."' 
       AND dimesion_weight>=".$select_weight." 
       AND (set_price != NULL
           OR set_price != ''
           OR set_price IS NOT NULL
           OR set_price <> '') 
       AND FIND_IN_SET ('".$select_from."',sel_countrys) 
       AND FIND_IN_SET ('".$select_to."',sel_countrys)  
       AND deleted='N' 
       AND status='Y' "
     .$searching_condition
     .$height_condition
     .$width_condition
     .$girth_condition
     .$order_by;

答案 1 :(得分:0)

表达式:

set_price != ''
set_priceNULL时,

未评估为真。

我怀疑你可能不想要空格:

trim(set_price) <> ''

如果需要,您可以添加and set_price is not null,但这是不必要的。