使用具有多个表连接的聚合函数的问题
我的目标是创建一个聚合函数(总和)并对元素进行分组,但出现错误,
这是我完成的所有步骤
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:当我按关键字删除分组并对所有选择求和时,它会起作用
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_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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
GROUP BY
放在WHERE
之后。Put the
GROUP BY
after theWHERE
.