遇到Visual Studio的SQL语法错误

时间:2016-10-06 12:21:45

标签: c# mysql visual-studio mysql-workbench

我收到此错误:

  

SELECT子句中的错误:'='附近的表达式。缺少FROM子句。 SELECT子句中的错误:','附近的表达式。无法解析查询文本。

 SELECT Sum(subhearingsched.conclusion = 'Conciliated'
       AND cases.nature_of_case = 'Criminal') AS criminalconciliated,
   Sum(subhearingsched.conclusion = 'Repudiated'
       AND cases.nature_of_case = 'Criminal') AS criminalrepudiated,
   Sum(subhearingsched.conclusion = 'Arbitrated'
       AND cases.nature_of_case = 'Criminal') AS criminalarbitrated,
   Sum(subhearingsched.conclusion = 'Certified'
       AND cases.nature_of_case = 'Criminal') AS criminalcertified,
   Sum(subhearingsched.conclusion = 'Dismissed'
       AND cases.nature_of_case = 'Criminal') AS criminaldismissed,
   Sum(subhearingsched.conclusion = 'Forwarded'
       AND cases.nature_of_case = 'Criminal') AS criminalforwarded,
   Sum(subhearingsched.conclusion = 'Mediated'
       AND cases.nature_of_case = 'Criminal') AS criminalmediated,
   Sum(subhearingsched.conclusion = 'Conciliated'
       AND cases.nature_of_case = 'Civil')    AS civilconciliated,
   Sum(subhearingsched.conclusion = 'Repudiated'
       AND cases.nature_of_case = 'Civil')    AS civilrepudiated,
   Sum(subhearingsched.conclusion = 'Arbitrated'
       AND cases.nature_of_case = 'Civil')    AS civilarbitrated,
   Sum(subhearingsched.conclusion = 'Certified'
       AND cases.nature_of_case = 'Civil')    AS civilcertified,
   Sum(subhearingsched.conclusion = 'Dismissed'
       AND cases.nature_of_case = 'Civil')    AS civilldismissed,
   Sum(subhearingsched.conclusion = 'Forwarded'
       AND cases.nature_of_case = 'Civil')    AS civilforwarded,
   Sum(subhearingsched.conclusion = 'Mediated'
       AND cases.nature_of_case = 'Civil')    AS civilmediated,
   Sum(subhearingsched.conclusion = 'Conciliated'
       AND cases.nature_of_case = 'Others')   AS othersconciliated,
   Sum(subhearingsched.conclusion = 'Repudiated'
       AND cases.nature_of_case = 'Others')   AS othersrepudiated,
   Sum(subhearingsched.conclusion = 'Arbitrated'
       AND cases.nature_of_case = 'Others')   AS othersarbitrated,
   Sum(subhearingsched.conclusion = 'Certified'
       AND cases.nature_of_case = 'Others')   AS otherscertified,
   Sum(subhearingsched.conclusion = 'Dismissed'
       AND cases.nature_of_case = 'Others')   AS othersdismissed,
   Sum(subhearingsched.conclusion = 'Forwarded'
       AND cases.nature_of_case = 'Others')   AS othersforwarded,
   Sum(subhearingsched.conclusion = 'Mediated'
       AND cases.nature_of_case = 'Others')   AS othersmediated
  FROM   subhearingsched
   JOIN cases
     ON ( subhearingsched.caseno = cases.caseno )  

这是我的查询,当我在visual studio查询构建器中运行时,它显示了一个解析错误,但它在mysql工作台中运行得很好,这也发生在我的其他查询中,在mysql工作台中完全正常但在visual studio中没有

1 个答案:

答案 0 :(得分:0)

不确定您使用的语法是否是MySQL方言(因此可以在MySQL工作台中使用),但最好使用标准SQL,在“SUM”中使用“CASE”,如下所示:

SELECT Sum(CASE WHEN subhearingsched.conclusion = 'Conciliated'
       AND cases.nature_of_case = 'Criminal' THEN 1 ELSE 0 END) AS criminalconciliated
  FROM   subhearingsched
   JOIN cases
     ON ( subhearingsched.caseno = cases.caseno )