0

尝试添加仅显示上一个日历月的记录的子查询。它需要检查的列称为 DateOfCheck 并且是一个自动时间戳列。

我有这个:

$SelectedMonth = "
  select *
  from   Data_Table
  where  DateOfCheck >= date_sub(curdate(), interval 1 month)
     and DateOfCheck <= date_sub(curdate(), interval 1 day)
";

并且当我将它放在主查询中时出现语法错误。

另外我认为(我是菜鸟)这只是从今天开始的最后一个月,当我需要它来查看最后一个日历月时。

我还需要一个单独的子查询来调用当前日历月中的所有记录。

好的,完整的查询(我知道这可能是非常低效的代码——正如我所说,我是一个试图拼凑一些东西的菜鸟!)如下,它工作正常,直到我尝试输入日期上面的子查询。

$Area = $_POST['Area'];
$product = $_POST['Product'];
$AverageScore = ("ROUND(AVG(Score),1)AS 'Avg <br/> Score'");
$AverageAutofails = ("ROUND(AVG(Autofails),1)AS 'Autofails <br/> per Check'");
$ProductTotal = "SELECT (COUNT (CA001Result) from Data_Table WHERE Product ='$product')";




$SelectedMonth = "select * from Data_Table where DateOfCheck >= date_sub(curdate(), interval 1 month) and DateOfCheck <= date_sub(curdate(), interval 1 day)"; 
ECHO $SelectedMonth;

if ($product == "All"){


$CA001 ="ROUND ((SELECT 100 * SUM(IF(CA001Result='Fail', 1, 0)) / COUNT(CA001Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>001'";

$CA002 ="ROUND ((SELECT 100 * SUM(IF(CA002Result='Fail', 1, 0)) / COUNT(CA002Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>002 '";

$CA003 ="ROUND ((SELECT 100 * SUM(IF(CA002Result='Fail', 1, 0)) / COUNT(CA002Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>003 '";

$CA004 ="ROUND ((SELECT 100 * SUM(IF(CA004Result='Fail', 1, 0)) / COUNT(CA004Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>004 '";

$CA005 ="ROUND ((SELECT 100 * SUM(IF(CA005Result='Fail', 1, 0)) / COUNT(CA005Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>005 '";

$CA006 ="ROUND ((SELECT 100 * SUM(IF(CA006Result='Fail', 1, 0)) / COUNT(CA006Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>006 '";

$CA007 ="ROUND ((SELECT 100 * SUM(IF(CA007Result='Fail', 1, 0)) / COUNT(CA007Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>007 '";

$CA008 ="ROUND ((SELECT 100 * SUM(IF(CA008Result='Fail', 1, 0)) / COUNT(CA008Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>008 '";

$CA009 ="ROUND ((SELECT 100 * SUM(IF(CA009Result='Fail', 1, 0)) / COUNT(CA009Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>009 '";

$CA010 ="ROUND ((SELECT 100 * SUM(IF(CA010Result='Fail', 1, 0)) / COUNT(CA010Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>010 '";


$CA011 ="ROUND ((SELECT 100 * SUM(IF(CA011Result='Fail', 1, 0)) / COUNT(CA011Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>011 '";

$CA012 ="ROUND ((SELECT 100 * SUM(IF(CA012Result='Fail', 1, 0)) / COUNT(CA012Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA0<br/>12 '";

$CA013 ="ROUND ((SELECT 100 * SUM(IF(CA013Result='Fail', 1, 0)) / COUNT(CA013Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>013 '";

$CA014 ="ROUND ((SELECT 100 * SUM(IF(CA014Result='Fail', 1, 0)) / COUNT(CA014Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>014 '";

$CA015 ="ROUND ((SELECT 100 * SUM(IF(CA015Result='Fail', 1, 0)) / COUNT(CA015Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>015 '";

$CA016 ="ROUND ((SELECT 100 * SUM(IF(CA016Result='Fail', 1, 0)) / COUNT(CA016Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>016 '";

$CA017 ="ROUND ((SELECT 100 * SUM(IF(CA017Result='Fail', 1, 0)) / COUNT(CA017Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA0<br/>17 '";

$CA018 ="ROUND ((SELECT 100 * SUM(IF(CA018Result='Fail', 1, 0)) / COUNT(CA018Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>018 '";

$CA019 ="ROUND ((SELECT 100 * SUM(IF(CA019Result='Fail', 1, 0)) / COUNT(CA019Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>019 '";

$CA020 ="ROUND ((SELECT 100 * SUM(IF(CA020Result='Fail', 1, 0)) / COUNT(CA020Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>020 '";

$CA021 ="ROUND ((SELECT 100 * SUM(IF(CA021Result='Fail', 1, 0)) / COUNT(CA021Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>021 '";

$CA022 ="ROUND ((SELECT 100 * SUM(IF(CA022Result='Fail', 1, 0)) / COUNT(CA022Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>022 '";

$CA023 ="ROUND ((SELECT 100 * SUM(IF(CA023Result='Fail', 1, 0)) / COUNT(CA023Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>023 '";

$CA024 ="ROUND ((SELECT 100 * SUM(IF(CA024Result='Fail', 1, 0)) / COUNT(CA024Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>024 '";


$CA025 ="ROUND ((SELECT 100 * SUM(IF(CA025Result='Fail', 1, 0)) / COUNT(CA025Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>025 '";

$CA026 ="ROUND ((SELECT 100 * SUM(IF(CA026Result='Fail', 1, 0)) / COUNT(CA026Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>026 '";

$CA027 ="ROUND ((SELECT 100 * SUM(IF(CA027Result='Fail', 1, 0)) / COUNT(CA027Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>027 '";

$CA028 ="ROUND ((SELECT 100 * SUM(IF(CA028Result='Fail', 1, 0)) / COUNT(CA028Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>028 '";

}
else{

$CA001 ="ROUND ((SELECT 100 * SUM(IF(CA001Result='Fail', 1, 0)) / COUNT(CA001Result) 
FROM Data_Table
WHERE  Area='$Area' AND Product = '$product'),0) as 'CA<br/>001 '";

$CA002 ="ROUND ((SELECT 100 * SUM(IF(CA002Result='Fail', 1, 0)) / COUNT(CA002Result) 
FROM Data_Table
WHERE  Area='$Area' AND Product = '$product'),0) as 'CA<br/>002 '";

$CA003 ="ROUND ((SELECT 100 * SUM(IF(CA003Result='Fail', 1, 0)) / COUNT(CA003Result) 
FROM Data_Table
WHERE  Area='$Area' AND Product = '$product'),0) as 'CA<br/>003 '";

$CA004 ="ROUND ((SELECT 100 * SUM(IF(CA004Result='Fail', 1, 0)) / COUNT(CA004Result) 
FROM Data_Table
WHERE  Area='$Area' AND Product = '$product'),0) as 'CA<br/>004 '";


$CA005 ="ROUND ((SELECT 100 * SUM(IF(CA005Result='Fail', 1, 0)) / COUNT(CA005Result) 
FROM Data_Table
WHERE  Area='$Area' AND Product = '$product'),0) as 'CA<br/>005 '";

$CA006 ="ROUND ((SELECT 100 * SUM(IF(CA006Result='Fail', 1, 0)) / COUNT(CA006Result) 
FROM Data_Table
WHERE  Area='$Area' AND Product = '$product'),0) as 'CA<br/>006 '";


$CA007 ="ROUND ((SELECT 100 * SUM(IF(CA007Result='Fail', 1, 0)) / COUNT(CA007Result) 
FROM Data_Table
WHERE  Area='$Area' AND Product = '$product'),0) as 'CA<br/>007 '";

$CA008 ="ROUND ((SELECT 100 * SUM(IF(CA008Result='Fail', 1, 0)) / COUNT(CA008Result) 
FROM Data_Table
WHERE  Area='$Area' AND Product = '$product'),0) as 'CA<br/>008 '";

$CA009 ="ROUND ((SELECT 100 * SUM(IF(CA009Result='Fail', 1, 0)) / COUNT(CA009Result) 
FROM Data_Table
WHERE  Area='$Area' AND Product = '$product'),0) as 'CA<br/>009 '";

$CA010 ="ROUND ((SELECT 100 * SUM(IF(CA010Result='Fail', 1, 0)) / COUNT(CA010Result) 
FROM Data_Table
WHERE  Area='$Area' AND Product = '$product'),0) as 'CA<br/>010 '";

$CA011 ="ROUND ((SELECT 100 * SUM(IF(CA011Result='Fail', 1, 0)) / COUNT(CA011Result) 
FROM Data_Table
WHERE  Area='$Area' AND Product = '$product'),0) as 'CA<br/>011 '";

$CA012 ="ROUND ((SELECT 100 * SUM(IF(CA012Result='Fail', 1, 0)) / COUNT(CA012Result) 
FROM Data_Table
WHERE  Area='$Area'),0) as 'CA<br/>012 '";

$CA013 ="ROUND ((SELECT 100 * SUM(IF(CA013Result='Fail', 1, 0)) / COUNT(CA013Result) 
FROM Data_Table
WHERE  Area='$Area' AND Product = '$product'),0) as 'CA<br/>013 '";

$CA014 ="ROUND ((SELECT 100 * SUM(IF(CA014Result='Fail', 1, 0)) / COUNT(CA014Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>014 '";

$CA015 ="ROUND ((SELECT 100 * SUM(IF(CA015Result='Fail', 1, 0)) / COUNT(CA015Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>015 '";

$CA016 ="ROUND ((SELECT 100 * SUM(IF(CA016Result='Fail', 1, 0)) / COUNT(CA016Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>016 '";

$CA017 ="ROUND ((SELECT 100 * SUM(IF(CA017Result='Fail', 1, 0)) / COUNT(CA017Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>017 '";

$CA018 ="ROUND ((SELECT 100 * SUM(IF(CA018Result='Fail', 1, 0)) / COUNT(CA018Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>018 '";

$CA019 ="ROUND ((SELECT 100 * SUM(IF(CA019Result='Fail', 1, 0)) / COUNT(CA019Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>019 '";

$CA020 ="ROUND ((SELECT 100 * SUM(IF(CA020Result='Fail', 1, 0)) / COUNT(CA020Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>020 '";

$CA021 ="ROUND ((SELECT 100 * SUM(IF(CA021Result='Fail', 1, 0)) / COUNT(CA021Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>021 '";

$CA022 ="ROUND ((SELECT 100 * SUM(IF(CA022Result='Fail', 1, 0)) / COUNT(CA022Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>022 '";

$CA023 ="ROUND ((SELECT 100 * SUM(IF(CA023Result='Fail', 1, 0)) / COUNT(CA023Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>023 '";

$CA024 ="ROUND ((SELECT 100 * SUM(IF(CA024Result='Fail', 1, 0)) / COUNT(CA024Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>024 '";


$CA025 ="ROUND ((SELECT 100 * SUM(IF(CA025Result='Fail', 1, 0)) / COUNT(CA025Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>025 '";

$CA026 ="ROUND ((SELECT 100 * SUM(IF(CA026Result='Fail', 1, 0)) / COUNT(CA026Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>026 '";

$CA027 ="ROUND ((SELECT 100 * SUM(IF(CA027Result='Fail', 1, 0)) / COUNT(CA027Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>027 '";

$CA028 ="ROUND ((SELECT 100 * SUM(IF(CA028Result='Fail', 1, 0)) / COUNT(CA028Result) 
FROM Data_Table
WHERE  Area='$Area'AND Product = '$product'),0) as 'CA<br/>028 '";



}

if ($product == "All"){
echo SQLResultTable2(" SELECT $SelectedMonth $CA001, $CA002, $CA003, $CA004, $CA005, $CA006, $CA007, $CA008, $CA009, $CA010, $CA011, $CA012, $CA013, $CA014, $CA015, $CA016, $CA017, $CA018, $CA019, $CA020,$CA021, $CA022, $CA023, $CA024, $CA025, $CA026, $CA027, $CA028 FROM Data_Table WHERE Area='$Area' GROUP BY Area");}
else{
    echo SQLResultTable2("SELECT $SelectedMonth, $CA001, $CA002, $CA003, $CA004, $CA005, $CA006, $CA007, $CA008, $CA009, $CA010, $CA011, $CA012, $CA013, $CA014, $CA015, $CA016, $CA017, $CA018, $CA019, $CA020,$CA021, $CA022, $CA023, $CA024, $CA025, $CA026, $CA027, $CA028 FROM Data_Table WHERE Area='$Area' GROUP BY Area");}

?>

任何帮助表示赞赏,干杯!

4

2 回答 2

2

问题是最终查询将以“SELECT SELECT”开头。你像这样组装它:

"SELECT $SelectedMonth, $CA001 ..."

$selectedMonth开始:

"select * from Data_Table where ..."

所以你得到:

"SELECT select * from Data_Table where ..."

另一个问题是,这是您唯一选择多个列的子查询,这使得语法更难集成到如此庞大的查询中。整个事情应该重新设计成一个大的单个 SELECT,因为您只从一个表中进行选择。这是一个非常快速的想法(一个选择,没有子查询):

$cols[] = "ROUND(AVG(Score),1)AS 'Avg <br/> Score'";
$cols[] = "ROUND(AVG(Autofails),1)AS 'Autofails <br/> per Check'";
$cols[] = "(COUNT (CA001Result)";

// Note: %03i will format 3 as 003
$columnFormat = "100 * SUM(IF(CA%03iResult='Fail', 1, 0)) / COUNT(CA%03iResult) AS CA%03i";
for($i = 1; $i <=28; $i++) {
  $cols[] = sprintf($columnFormat, $i, $i, $i);
}

$fieldList = implode(', ', $cols);
$sql = "SELECT " . $fieldList;
$sql .= " FROM Data_table WHERE Area='$Area' AND DateOfCheck BETWEEN CURDATE() - INTERVAL 1 MONTH AND CURDATE() - INTERVAL 1 DAY";
if($product != 'ALL') {
  $sql .= " AND Product = '$product'";
}
$sql .= " GROUP BY Area";

理由:上面的代码,用最简单的术语来说,是这样的:

  1. 构建一个“列”的 PHP 数组以选择 ( $cols)
  2. 组装这些列 ( implode)
  3. 通过连接适当地添加您的 WHERE 广告 GROUP BY 子句。

最棘手的部分是 sprintf,在它的 php 手册页上有很好的解释。本质上,它解析一个字符串并在其中放置一个格式化版本的变量。格式说明符说“这个%i参数将是一个整数”。但是,如果没有额外的处理,您的列别名将是“CA1”而不是“CA001”。幸运的是,sprintf 有我们需要的东西,它%03i说“使 int 至少有 3 位宽,并用 0(而不是空格)填充它”。因此,例如:COUNT(CA%03iResult)当通过 23 时,将导致COUNT(CA023Result).

强烈建议您为自己的剧本考虑这门课程。它的性能要好几个数量级,而且更不容易出错(例如,由于您的执行分叉在两个地方,当您在其中一个路径中引入 SQL 错误时,您可能不会立即注意到(例如,您的 $product =ALL OP 中的查询缺少另一个逗号,简单的疏忽,但如果您不经常运行自动化测试,很容易错过)。

于 2012-08-30T14:10:03.733 回答
0

关于您的子查询和上一个日历月的问题,您可以使用它date_format(curdate() - interval 1 month,'%Y-%m-01 00:00:00')来获取整个上个月,如下所示:

select *
  from   Data_Table
  where DateOfCheck between date_format(curdate() - interval 1 month,'%Y-%m-01 00:00:00')
     and date_sub(curdate(), interval 1 day)
于 2012-08-30T14:04:55.727 回答