【问题标题】:problem using aggregate functions with multipul table joins使用具有多个表连接的聚合函数的问题
【发布时间】:2010-07-24 12:54:07
【问题描述】:

我的目标是做一个聚合函数(sum)并对元素进行分组,但是有一个错误

这是我已经完成的所有步骤

1- 第一步 代码

SELECT ca.question_id , ca.choice_0 ,ca.choice_1 ,ca.choice_2 ,ca.choice_3 ,ca.choice_4 ,q.headline_id  FROM closed_answers ca
                INNER JOIN questions q ON ca.question_id = q.id 
                INNER JOIN headline h ON q.headline_id = h.id
                INNER JOIN forms f ON h.form_id = f.id
                WHERE f.id = 2

结果

http://img717.imageshack.us/img717/685/firststep.png


2- 现在我想汇总选择并按标题 id 对它们进行分组,但是当我写的时候

SELECT sum(ca.choice_0) ,sum(ca.choice_1) ,sum(ca.choice_2) ,sum(ca.choice_3) ,sum(ca.choice_4) ,q.headline_id  FROM closed_answers ca

                INNER JOIN questions q ON ca.question_id = q.id 
                INNER JOIN headline h ON q.headline_id = h.id
                INNER JOIN forms f ON h.form_id = f.id
                GROUP BY q.headline_id
                WHERE f.id = 2

错误是

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE f.id = 2' at line 6

SELECT sum(ca.choice_0) ,sum(ca.choice_1) ,sum(ca.choice_2) ,sum(ca.choice_3) ,sum(ca.choice_4) ,q.headline_id FROM closed_answers ca INNER JOIN questions q ON ca.question_id = q.id INNER JOIN headline h ON q.headline_id = h.id INNER JOIN forms f ON h.form_id = f.id GROUP BY q.headline_id WHERE f.id = 2

PS:当我按关键字删除组并总结所有选择时,它会起作用

http://img203.imageshack.us/img203/8186/secondstepx.png

【问题讨论】:

    标签: sql join aggregate-functions mysql-error-1064


    【解决方案1】:

    GROUP BY 放在WHERE 之后。

    【讨论】:

      猜你喜欢
      • 2014-09-07
      • 2013-02-22
      • 2019-03-20
      • 1970-01-01
      • 2012-06-24
      • 2018-08-06
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多