带有,分组依据的LEFT OUTER JOIN出错

时间:2015-02-02 13:33:42

标签: mysql sql

我正在尝试执行此MYSQL QUERY,我尝试了很多方法,但是出现了错误

查询是:

SELECT COUNT(*) AS cnt 
FROM 
(  
   SELECT SUM(C.fail) AS FAIL_COUNT, A.riskclassifyAS riskclassify,A.logicaldel 
   FROM tc_details AS A 
   LEFT OUTER JOIN tce_master AS B ON A.tc_name=B.tce_tcname AND A.project=B.project 
   LEFT OUTER JOIN release_details AS C ON  A.tc_name=C.TC_id AND  A.project=C.project  
   GROUP BY A.TC_NAME, A.TC_Desc, A.project, A.tc_priority, A.criticality, B.tce_effectivness 
   HAVING A.project='JupiterQA' AND A.logicaldel=0 AND  
   (
      (criticality <3 OR criticality IS NULL) AND 
      (fail_count>0 OR B.tce_effectivness>0)
   );

错误是:

  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在''

附近使用正确的语法

你能告诉我在这个问题中我做错了什么。

2 个答案:

答案 0 :(得分:2)

你不能写

Select count(distinct A.TC_NAME, A.TC_Desc, 
       A.project, A.tc_priority, 
       A.criticality, B.tce_effectivness ) cnt
From tc_details AS A 
    LEFT JOIN tce_master AS B 
          ON A.tc_name=B.tce_tcname 
               AND A.project=B.project 
    LEFT JOIN release_details AS C 
          ON  A.tc_name=C.TC_id 
               AND  A.project=C.project
Where A.project='JupiterQA' AND A.logicaldel=0 
   AND coalesce(criticality, 0) < 3
   And (count > 0 Or B.tce_effectivness > 0)  

由于select子句中没有聚合函数(Sum(). Min(), Max()等),因此查询只不过是计算由group中的表达式定义的存储区数量。这相当于Count (Distinct )

...并且,由于您的having子句中没有聚合函数,因此这些谓词可以在Where子句中。

答案 1 :(得分:1)

您缺少子查询的别名和)

SELECT COUNT(*) AS cnt 
FROM 
(  
   SELECT SUM(C.fail) AS FAIL_COUNT, A.riskclassifyAS riskclassify,A.logicaldel 
   FROM tc_details AS A 
   LEFT OUTER JOIN tce_master AS B ON A.tc_name=B.tce_tcname AND A.project=B.project 
   LEFT OUTER JOIN release_details AS C ON  A.tc_name=C.TC_id AND  A.project=C.project  
   GROUP BY A.TC_NAME, A.TC_Desc, A.project, A.tc_priority, A.criticality, B.tce_effectivness 
   HAVING A.project='JupiterQA' AND A.logicaldel=0 AND  
   (
      (criticality <3 OR criticality IS NULL) AND 
      (fail_count>0 OR B.tce_effectivness>0)
   )
) tmp