创建仅显示没有空值的行的查询

时间:2014-06-27 19:47:23

标签: php mysql sql database

我正在尝试创建一个查询语句,该语句只会获取其列中没有空值的行。但我不知道该怎么做。

以下是查询语句:

    $query = "SELECT scrap_wafers.lot, scrap_wafers.wafer_cell, scrap_wafers.flow, ".
"scrap_wafers.route, scrap_wafers.scrap_date, scrap_wafers.scrap_comment, ".
"scrap_type_names.scrap_tag_name, scrap_wafers.engineer, scrap_wafers.disposition_date, ".
"scrap_wafers.disposition_comment, scrap_wafers.location, scrap_wafers.serial_sw, scrap_wafers.operator ".
"FROM scrap_wafers ".
"LEFT JOIN scrap_type_names ON scrap_type_names.serial_stn = scrap_wafers.serial_stn ".
"$cstring ".
"GROUP BY scrap_wafers.serial_sw ".
"ORDER BY scrap_wafers.scrap_date DESC ".
"LIMIT $numrecs";

$ criteria包括:

$criteria = array();
if (!empty($lot)) {
 $lotarray = explode(',', $lot);
 $lot_criteria_str = "(";
 foreach ($lotarray as $lottmp) {
  $lot_criteria_str = $lot_criteria_str . "scrap_wafers.lot like '%$lottmp%' OR ";
 } 
 $lot_criteria_str = substr($lot_criteria_str,0,-3); 
 $lot_criteria_str = $lot_criteria_str . ")";
 $criteria[] = $lot_criteria_str;
 }
if (!empty($wafer_cell)) {
 $waferarray = explode(',', $wafer_cell);
 $wafer_criteria_str = "(";
 foreach ($waferarray as $wafertmp) {
  $wafer_criteria_str = $wafer_criteria_str . "scrap_wafers.wafer_cell like '%$wafertmp%' OR ";
 } 
 $wafer_criteria_str = substr($wafer_criteria_str,0,-3); 
 $wafer_criteria_str = $wafer_criteria_str . ")";
 $criteria[] = $wafer_criteria_str;
 } 
    if (!empty($serial_flow)) $criteria[] = "scrap_wafers.serial_flow = '$serial_flow'";
    if (!empty($serial_route)) $criteria[] = "scrap_wafers.serial_route = '$serial_route'"; 
    if (!empty($operator)) $criteria[] = "scrap_wafers.operator = '$operator'";     
    if (!empty($serial_stn)) $criteria[] = "scrap_wafers.serial_stn = '$scrap_tag_name'";               
    if (!empty($scrap_date)) $criteria[] = "scrap_wafers.scrap_date = '$scrap_date'";   
    if (!empty($serial_step)) $criteria[] = "scrap_wafers.serial_step = '$serial_step'";    
    if (!empty($location)) $criteria[] = "scrap_wafers.location = '$location'";             
    if (!empty($completeness)) {
        if ($completeness == 'Default') { 
        // Do nothing
        }
        else if ($completeness == 'Incomplete') {
            $criteria[] = "scrap_wafers.disposition_date = null AND scrap_wafers.engineer = null AND ".
            "scrap_wafers.disposition_comment = null AND scrap_wafers.location = null AND  ".
            "scrap_wafers.serial_stn = null";
        }
        else if ($completeness == 'Complete') {
            $criteria[] = "scrap_wafers.disposition_date = not null AND scrap_wafers.engineer = not null AND ".
            "scrap_wafers.disposition_comment = not null AND scrap_wafers.location = not null AND  ".
            "scrap_wafers.serial_stn = not null";
        }
    }   
$cstring = "";
    if (count($criteria) > 0) {
        $cstring = "WHERE ".join(" AND ", $criteria);
    } else {
        $cstring = "WHERE 1=1 ";
    }

$ completeness是我试图过滤空值(不完整)或没有空值(完整)的地方。

1 个答案:

答案 0 :(得分:0)

您需要使用IS NULL,而不是= null

...

else if ($completeness == 'Incomplete') {
    $criteria[] = "scrap_wafers.disposition_date IS NULL AND scrap_wafers.engineer IS NULL AND ".
    "scrap_wafers.disposition_comment IS NULL AND scrap_wafers.location IS NULL AND  ".
    "scrap_wafers.serial_stn IS NULL";
}
else if ($completeness == 'Complete') {
    $criteria[] = "scrap_wafers.disposition_date IS NOT NULL AND scrap_wafers.engineer IS NOT NULL AND ".
    "scrap_wafers.disposition_comment IS NOT NULL AND scrap_wafers.location IS NOT NULL AND  ".
    "scrap_wafers.serial_stn IS NOT NULL";
}

...