2010-07-24 3 views
0

Mon but est de faire une fonction d'agrégation (somme) et regrouper les éléments, mais il y a une erreurà l'aide des fonctions d'agrégation avec table multipul rejoint

c'est toutes les étapes que je l'ai fait

1 - première étape 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_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 

le résultat

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


2- Maintenant, je veux agréger les choix et les regrouper par id titre, mais quand j'écris

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 

l'erreur est

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: ça fonctionne quand je supprime le groupe par mot-clé et somme tous les choix

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

Répondre

6

Placez le GROUP BY après le WHERE.