MYSQL日期/时间子查询上的语法错误

时间:2012-08-30 13:51:55

标签: mysql sql datetime

尝试添加仅显示上个日历月记录的子查询。它需要检查的列名为DateOfCheck,是一个自动TimeStamp列。

我有这个:

$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");}

?>

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

2 个答案:

答案 0 :(得分: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子句。
  4. 最棘手的部分是sprintf,在it's php manual page上有很好的解释。本质上,它解析一个字符串并在其中放置一个变量的格式化版本。 %i格式说明符表示“此参数将是整数”。但是,如果没有其他处理,您的列别名将为“CA1”而不是“CA001”。幸运的是,sprintf具有我们所需要的,%03i说“使int至少3位宽,并用0填充(而不是空格)”。因此,例如:COUNT(CA%03iResult)传递23时,将导致COUNT(CA023Result)

    强烈建议您为脚本考虑此课程。它的性能数量级更高,并且更不容易出错(例如,由于您在两个位置执行分支,当您在其中一个路径中引入SQL错误时,您可能不会立即注意到(例如,您的$ product) = OP中的所有查询都缺少另一个查询,简单的疏忽,但如果您没有经常运行自动化测试则很容易错过。)

答案 1 :(得分: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)