[英]I want to query from model of laravel to remove the columns that has only null values in laravel
[英]Make a query that only shows rows that has no null values
我正在嘗試創建一個查詢語句,該語句將僅捕獲其列中不包含空值的行。 但是我不確定該怎么做。
這是查詢語句:
$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是我嘗試過濾空值(不完整)或沒有空值(完整)的地方。
您需要使用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";
}
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.