开发者

problem using aggregate functions with multipul table joins

My goal is to make an aggregate function (sum) and group the elements , but there is an error

this is all the steps that i have done

1- first step code

SELECT ca.question_id , ca.choice_0 ,ca.choice_1 ,ca.choice_2 ,ca.choice_3 ,ca.choice_4 ,q.headline_id  FROM closed_an开发者_高级运维swers 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

the result

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


2- now i want to aggregate the choices and group them by headline id but when i write

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

the error is

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 : it works when i remove the group by keyword and sum all the choices

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


Put the GROUP BY after the WHERE.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜