使用具有多个表连接的聚合函数的问题

发布于 2024-09-11 23:17:30 字数 1833 浏览 6 评论 0原文

我的目标是创建一个聚合函数(总和)并对元素进行分组,但出现错误,

这是我完成的所有步骤

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月隐月明月朦胧 2024-09-18 23:17:30

GROUP BY 放在WHERE 之后。

Put the GROUP BY after the WHERE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文