尝试添加仅显示上一个日历月的记录的子查询。它需要检查的列称为 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");}
?>
任何帮助表示赞赏,干杯!