sql语法错误

g9icjywg  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(468)

我得到这个错误:
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 )

这是我的查询,当我在visualstudio query builder中运行它时,它显示了一个解析错误,但在mysql工作台中工作得非常好,而在mysql工作台中工作得非常好但在visualstudio中工作不好的其他查询也出现了这种情况。

ny6fqffe

ny6fqffe1#

不确定您使用的语法是否是mysql方言(因此在mysql workbench中工作),但最好使用标准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 )

相关问题